Asp.net 如何在DLL中添加web服务引用

Asp.net 如何在DLL中添加web服务引用,asp.net,web-services,dll,Asp.net,Web Services,Dll,我正在创建一个包含对web服务引用的DLL(我没有选择权),但我必须向使用该DLL的项目添加web服务引用 例如,我有一个名为API.DLL的DLL,它调用一个名为WebService.svc的web服务,我想在一个名为WinForm的项目中使用它。首先,我必须向API.DLL中的WebService.svc添加一个“服务引用”。然后,我向WinForm添加了一个引用API.DLL,但除非我在WinForm中也向WebService.svc添加一个服务引用,否则它不会工作 如何避免最后一步?是因

我正在创建一个包含对web服务引用的DLL(我没有选择权),但我必须向使用该DLL的项目添加web服务引用

例如,我有一个名为API.DLL的DLL,它调用一个名为WebService.svc的web服务,我想在一个名为WinForm的项目中使用它。首先,我必须向API.DLL中的WebService.svc添加一个“服务引用”。然后,我向WinForm添加了一个引用API.DLL,但除非我在WinForm中也向WebService.svc添加一个服务引用,否则它不会工作


如何避免最后一步?

是因为您需要服务引用,以便WinForm应用程序可以引用您的服务返回的类型


如果是这种情况,您可以将API dll转换为服务的包装器,将服务上可用的所有方法包装在API中,并对对象执行相同的操作(假设它们是您创建的类别)。显然,您必须将服务返回的所有对象转换为API中的对象。但一旦完成,就不需要在WinForm中引用服务。(可笑的是,我自己也在做类似的事情)。

你可能使用的是Web服务公开的类型,或者可能是你必须添加Web引用,以便将适当的连接信息添加到配置文件中。winform应用程序将不会以任何方式继承或使用DLL的配置文件,除非您为它准备了一些奇特的加载机制。换句话说,在DLL中,当您添加web引用时,它的配置文件获取有关如何连接到web服务的信息,但是当您在应用程序中使用DLL时,您的应用程序需要在其自己的配置文件中包含该信息,因此您必须添加web引用,以便生成信息

关于使用web引用公开的类型,我不确定这是否是您遇到的问题。我在DLL中遇到过这种情况。例如,SharpMap.dll声明了一个
SharpMapData
类,WrapperOfSharpMap.dll有一个名为
ProcessData(SharpMapData blah)

如果我编写WinForm应用程序并添加对WrapperOfSharpMap.dll的引用,我还必须添加对SharpMap.dll的引用,因为要调用
ProcessData
,我必须创建一个
SharpMapData
实例来传递给函数。换句话说,我正在使用SharpMap.dll中声明的类型,因此需要对其进行引用

要解决此问题,WrapperOfSharpMap.dll的创建者必须创建一个
WrapperSharpMapData
类,如下所示:

class WrapperSharpMapData
{
  private SharpMapData hiddenSharpMapData;

  //create proprerties to access data elements using standard CLR types or other wrapper types
}

您可以尝试查看,这允许您通过命令行为您的服务生成客户端代理代码。您应该能够获取它生成的代码文件,并将其简单地添加到DLL项目中。这可以让您完全绕过Visual Studio。

您的第一步是向自己证明这是可能的,然后调整您的项目

您可以下载可运行的解决方案

我只是通过这些步骤,列举了我的行动,以产生你想要的结果

Create a web app project (an thus a solution) named 'ReferencedWebService' Add a web service, just leave the default name and implementation Add a class library named 'ReferencedWebserviceAPI' Add Service Reference >Advanced >Add Web Reference>Web services in this solution >WebService1 >Add reference leaving name as 'localhost' Add a console application project named 'ReferencedWebserviceClient' To ReferencedWebserviceClient: Add Reference >Projects >ReferencedWebserviceAPI Add Reference >.Net >System.Web.Services In Program.cs replace Main: static void Main(string[] args) { var svc = new ReferencedWebserviceAPI.localhost.WebService1(); var result = svc.HelloWorld(); Console.WriteLine(result); Console.ReadLine(); } Set ReferencedWebserviceClient as startup project and run. Ok, this is simplest scenario. One issue you will have to deal with is that the default Service URL is hardcoded in the .dll, and it is set to a ported address on your dev machine. You will want to add a configuration parameter to your client project. The simplest and most portable way is to use an appSetting. To ReferencedWebserviceClient: Add Item >Application Configuration File Replace contents of App.Config with this, of course replace the value with the appropriate value on your machine. Add Reference >.Net >System.Configuration Now replace Main with this: static void Main(string[] args) { var svc = new ReferencedWebserviceAPI.localhost.WebService1 { Url = ConfigurationManager.AppSettings["serviceUrl"] }; var result = svc.HelloWorld(); Console.WriteLine(result); Console.ReadLine(); } 创建一个名为“ReferenceWebService”的web应用程序项目(即解决方案) 添加一个web服务,只需保留默认名称和实现 添加名为“ReferenceWebServiceAPI”的类库 添加服务引用 >先进的 >在此解决方案中添加Web引用>Web服务 >网络服务1 >添加引用,保留名称为“localhost” 添加名为“ReferenceWebServiceClient”的控制台应用程序项目 要引用WebServiceClient,请执行以下操作: 添加引用 >计划 >参考WebServiceAPI 添加引用 >.Net >System.Web.Services 在Program.cs中,替换Main: 静态void Main(字符串[]参数) { var svc=newreferencedWebServiceAPI.localhost.WebService1(); var result=svc.HelloWorld(); 控制台写入线(结果); Console.ReadLine(); } 将ReferenceWebServiceClient设置为启动项目并运行。 好的,这是最简单的场景。您必须处理的一个问题是,默认的服务URL是在.dll中硬编码的, 并且它被设置为开发机器上的一个移植地址。 您需要将配置参数添加到客户端项目中。最简单和最可移植的方法是使用appSetting。 要引用WebServiceClient,请执行以下操作: 添加项 >应用程序配置文件 用这个替换App.Config的内容,当然用计算机上的适当值替换该值。 添加引用 >.Net >系统配置 现在将Main替换为以下内容: 静态void Main(字符串[]参数) { var svc=new ReferencedWebserviceAPI.localhost.WebService1 { Url=ConfigurationManager.AppSettings[“serviceUrl”] }; var result=svc.HelloWorld(); 控制台写入线(结果); Console.ReadLine(); } 这是在可再发行的.dll中嵌入服务的基线

尝试将此模型应用到您当前的项目中,看看它是如何为您工作的

如果你仍然有问题,你肯定有一个参考问题,应该从这个角度来看待它


希望这有助于

在客户的项目中右键单击添加服务引用,以添加方法和类

然后,要访问该服务,您可以执行类似于dll的操作

EndpointIdentity spn = EndpointIdentity.CreateSpnIdentity("host/mikev-ws");
Uri uri = new Uri("http://example.com/test.asmx");
var address = new EndpointAddress(uri, spn);
BasicHttpBinding basic = new BasicHttpBinding();
basic.Name = "bindingName";

var client = new MyService.MyWebServiceSoapClient(basic, address);
client.HelloWorld();

您不必将服务引用添加到WinForm。。。您看到的错误是什么?System.InvalidoOperationException My.NET是法语(…叹气…),但它说的是“找不到服务引用”