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

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

问题描述

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



示例,我有一个名为API.DLL的DLL,调用一个名为WebService.svc的Web服务,我想在一个名为WinForm的项目中使用。首先,我必须在API.DLL中的WebService.svc中添加一个服务参考。然后,我将参考API.DLL添加到WinForm,但是它不起作用,除非我还在WinForm中添加了WebService.svc的服务引用。



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

解决方案

您的第一步是向自己证明这是可行的,然后适应您的项目。 / 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

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

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