dsi*_*ard 6 asp.net dll web-services
我正在创建一个引用Web服务的DLL(我没有选择这样做)但我必须添加Web服务引用到使用DLL才能工作的项目.
例如,我有一个名为API.DLL的DLL,它调用一个名为WebService.svc的Web服务,我想在一个名为WinForm的项目中使用它.首先,我必须在API.DLL中向WebService.svc添加"服务引用".然后,我将一个引用API.DLL添加到WinForm但它不起作用,除非我还在WinForm中添加对WebService.svc的服务引用.
我该怎么做才能避免最后一步?
您的第一步是向自己证明这是可能的,然后调整您的项目.
你可以在这里下载可运行的解决方案.
我刚刚完成了这些步骤并列举了我的行动,以产生您想要的结果.
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();
}
这是在可再发行的.dll中嵌入服务的基线.
尝试将此模型应用于您当前的项目,看看它是如何工作的.
如果你仍然有问题,你肯定有一个参考问题,应该从这个角度开始看.
希望这可以帮助
您可能正在使用Web服务公开的类型,或者您可能需要添加Web引用,以便将适当的连接信息添加到配置文件中.winform应用程序不会以任何方式继承或使用DLL的配置文件,除非你为它烹饪一些花哨的加载机制.换句话说,在添加Web引用的DLL中,它的配置文件获取有关如何连接到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
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17570 次 |
| 最近记录: |