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

标签 asp.net web-services dll

我正在创建一个引用 Web 服务的 DLL(我没有选择这样做),但我必须将 Web 服务引用添加到使用该 DLL 的项目中才能正常工作。

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

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

最佳答案

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

您可以下载可运行的解决方案 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 中嵌入服务的基准。

尝试将此模型应用于您当前的项目,看看它是否适合您。

如果您仍然遇到问题,那么您肯定有一个引用问题,应该开始从这个角度来看待它。

希望这有帮助

关于asp.net - 如何在 DLL 中添加 Web 服务引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2247798/

相关文章:

asp.net - 在 dotnet mvc 5 中,如何将服务注入(inject) View ?

java.lang.IllegalArgumentException,但参数似乎是有效类型

c# - 从 C# 中的 dll 加载特定类

c# - C#中的命名问题

asp.net - 全文搜索最接近的匹配项

javascript - IE 10 中的 Telerik DatePicker 不工作

c# - 即使将身份验证模式设置为 'Windows',ASP.Net MVC4 User.Identity.Name 也会变空

java - 是否可以在 JAR 中而不是在 WAR 中创建 JAX-WS 服务?

java - 非静态 webapp 别名

c++ - 如何将其他人提供的dll和lib链接到c++程序