这一讲再说说导入dll的事情。下文说的“非托管dll”,说白了就是用C++语言编写源码后生成的dll。那么“托管”的就是说C#的。
导入非托管dll
一、制作非托管dll
制作过程略。重要的是,将dll文件拷贝到WebService项目的两个地方:
1.项目目录的“bin”文件夹中,与项目自身的dll文件在一起:
2.网站目录中,与项目自身的dll文件在一起:
如果此dll还依赖其他dll,也要跟着拷过来。
二、导入
将WebService1.asmx代码修改成这样(下面标了“///////////////////////////”的部分是这次修改的):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Runtime.InteropServices;
namespace WebApplication1
{
[WebService(Namespace = "http://www.myweb.com/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public class WebService1 : System.Web.Services.WebService
{
const string dllPath = "dllProject.dll"; ///////////////////////////
[DllImport(dllPath, EntryPoint = "intToInt")] ///////////////////////////
extern static int intToInt(int a, int b); ///////////////////////////
[WebMethod] ///////////////////////////
public int addInt(int x, int y) ///////////////////////////
{ ///////////////////////////
return intToInt(x, y); ///////////////////////////
} ///////////////////////////
[WebMethod]
public string addStr(string x, string y)
{
return x + y;
}
}
}
保存,重新生成,发布(再发布,就不用设置配置文件了,直接点“发布”按钮就行了)。
在刚才带“192.168.1.100”的那个浏览器中,刷新一下,可以看到网页已经变了。
关闭此浏览器,回到IIS中,右键点“WebService1.asmx”,在弹出菜单中选择“浏览”,重新打开网页,可以看到它仍然是改变了的。
这就是说,重新发布不再需要经过IIS。
点“addInt”按钮,现在网页调用的不仅仅是C#的方法,还有C++写的非托管dll中的方法。这个dll已经被拷入项目目录、网站目录,以后也要跟着去互联网服务器。
在C#项目中调用WebService
你用C#写了一个程序,它时不时需要访问WebService,那么在VisualStudio中这样处理这个C#程序:
在解决方案资源管理器中,在项目名称上点右键,在弹出菜单中选择“添加服务引用”。
在弹出窗口中,把这个WebService的互联网访问路径写进去(比如是“http://111.222.333.444/WebService1.asmx”),在“命名空间”中填一个本程序尚未使用的名称。点“确定”按钮,然后按提示进行。
随后,在项目中会出现“Service References”文件夹、“app.config”文件。
app.config需要改成下面的样子:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="WebFunctionsSoap" maxBufferPoolSize="2147483647"
maxReceivedMessageSize="2147483647" useDefaultWebProxy="true" />
</basicHttpBinding>
<customBinding>
<binding name="WebFunctionsSoap12">
<textMessageEncoding messageVersion="Soap12" />
<httpTransport />
</binding>
<binding name="WebFunctionsSoap121">
<textMessageEncoding messageVersion="Soap12" />
<httpTransport />
</binding>
</customBinding>
</bindings>
<client>
<endpoint address="http://111.222.333.444/WebService1.asmx" binding="customBinding"
bindingConfiguration="WebFunctionsSoap12" contract="ServiceReference1.WebFunctionsSoap"
name="WebFunctionsSoap12" />
<endpoint address="http://111.222.333.444/WebService1.asmx" binding="basicHttpBinding"
bindingConfiguration="WebFunctionsSoap" contract="ServiceReference1.WebFunctionsSoap"
name="WebFunctionsSoap" />
<endpoint address="http://111.222.333.444/WebService1.asmx" binding="customBinding"
bindingConfiguration="WebFunctionsSoap121" contract="ServiceReference1.WebFunctionsSoap"
name="WebFunctionsSoap121" />
</client>
</system.serviceModel>
</configuration>
然后把里面的“http://119.23.213.71/WebFunctions.asmx”全部换成你自己的地址。
然后,调用此WebService的函数,形如:
void visitWebService(string x, string y)
{
try
{
ServiceReference1.WebFunctionsSoapClient w =
new ServiceReference1.WebFunctionsSoapClient("WebFunctionsSoap");
string result = w.addStr(x, y);
}
catch (Exception e)
{
}
}