如何在DLL中添加Web服务引用 [英] How to add a web service reference in a DLL

查看:409
本文介绍了如何在DLL中添加Web服务引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建与Web服务的引用的DLL(我没有选择这样做),但我必须Web服务引用添加到使用该DLL为它工作的项目。

I'm creating a DLL with a reference to web services (I don't have the choice to do so) but I have to add web service references to the project that uses the DLL for it to work.

例如,我有叫API.DLL的DLL调用名为WebService.svc一个Web服务,我想在一个名为WinForm的项目中使用。首先,我要添加一个服务引用,以WebService.svc在API.DLL。然后,我添加引用API.DLL到WinForm的,但它不工作,除非我在winform的服务引用添加到WebService.svc。

Example, I have the DLL called API.DLL that calls a web service called WebService.svc that I want to use in a project called WinForm. First, I have to add a "Service Reference" to WebService.svc in API.DLL. Then, I add a reference API.DLL to WinForm but it doesn't work unless I also add a service reference to WebService.svc in WinForm.

我能做些什么,以避免最后一步?

What can I do to avoid that last step?

推荐答案

您的第一步是要证明自己,这是可能的,然后适应您的项目。

Your first step is to prove to yourself that this is possible and then adapt your project.

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

you can download the runnable solution here.

我刚刚经历的步骤去和枚举我的行动产生你想要的结果。

I just went through the steps and enumerated my actions to produce the results you desire.


    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文件中嵌入服务基线。

This is the baseline for embedding services in a redistributable .dll.

尝试将此模式应用到当前项目,看看它是如何工作适合你。

Try to apply this model to your current project and see how that works for you.

如果您仍然有问题,你最肯定有一个参考的问题,应该从这个角度看它。

If you still have problems, you most definitely have a reference issue and should start looking at it from that perspective.

希望这有助于

这篇关于如何在DLL中添加Web服务引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆