加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

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

发布时间:2020-12-16 00:12:42 所属栏目:asp.Net 来源:网络整理
导读:我正在创建一个引用Web服务的DLL(我没有选择这样做)但我必须向使用DLL的项目添加Web服务引用才能使用它. 例如,我有一个名为API.DLL的DLL,它调用一个名为WebService.svc的Web服务,我想在一个名为WinForm的项目中使用它.首先,我必须在API.DLL中向WebService.sv
我正在创建一个引用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中嵌入服务的基线.

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

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

希望这可以帮助

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读