且构网

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

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

更新时间:2023-01-04 09:28:55

您的第一步是向自己证明这是可行的,然后适应您的项目。 / p>

您可以下载可运行的解决方案 here



我刚刚经历了这些步骤并枚举我的动作以产生您想要的结果。

 
创建一个名为ReferencedWebService的web应用程序项目(一个解决方案)
添加一个Web服务,只需保留默认名称和实现
添加一个名为ReferencedWebserviceAPI的类库
添加服务参考
>高级
>添加Web裁剪nce>此解决方案中的Web服务
> WebService1
>添加引用离开名称为localhost
添加一个名为ReferencedWebserviceClient的控制台应用程序项目
ToReferencedWebserviceClient:
添加参考
>项目
> ReferencedWebserviceAPI
添加参考
> .Net
> System.Web.Services

在Program.cs替换主要:

static void Main(string [] args)
{
var svc = new ReferencedWebserviceAPI.localhost.WebService1();
var result = svc.HelloWorld();
Console.WriteLine(result);
Console.ReadLine();
}


将ReferencedWebserviceClient设置为启动项目并运行。

好​​的,这是最简单的方案。您必须处理的一个问题是默认的服务URL在.dll
中被硬编码,并且它被设置为您的开发机器上的移植地址。

您将要为客户端项目添加一个配置参数。最简单和最便携的方式是使用appSetting。

ToReferencedWebserviceClient:
添加项
>应用程序配置文件

将这个App.Config的内容替换为适当的值机器上的价值。









添加参考
> .Net
> System.Configuration

现在用这个替换Main:

static void Main(string [] args)
{
var svc =新的ReferencedWebserviceAPI.localhost.WebService1
{
Url = ConfigurationManager.AppSettings [serviceUrl]
};
var result = svc.HelloWorld();
Console.WriteLine(result);
Console.ReadLine();
}

这是在可再发行的.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.

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();
    }

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.

Hope this helps