且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在DLL中添加Web服务引用

更新时间:2023-01-04 09:51:49

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

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.

希望这有助于