asp.net – 如何在DLL中添加Web服务引用

前端之家收集整理的这篇文章主要介绍了asp.net – 如何在DLL中添加Web服务引用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在创建一个引用Web服务的DLL(我没有选择这样做)但我必须向使用DLL的项目添加Web服务引用才能使用它.

例如,我有一个名为API.DLL的DLL,它调用一个名为WebService.svc的Web服务,我想在一个名为WinForm的项目中使用它.首先,我必须在API.DLL中向WebService.svc添加“服务引用”.然后,我将一个引用API.DLL添加到WinForm但它不起作用,除非我还在WinForm中添加对WebService.svc的服务引用.

我该怎么做才能避免最后一步?

解决方法

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

你可以下载runnable解决方here.

我刚刚完成了这些步骤并列举了我的行动以产生你想要的结果.

    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中嵌入服务的基线.

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

如果你仍然有问题,你肯定有一个参考问题,应该从这个角度开始看.

希望这可以帮助

原文链接:https://www.f2er.com/aspnet/251703.html

猜你在找的asp.Net相关文章