在Visual Studio中使用动态根URL生成Web服务

时间:2018-10-23 13:30:44

标签: c# visual-studio web-services soap

我想创建一个与两个SOAP Web服务通信的C#应用​​程序。这些Web服务(WSDL文件)使用相同的URL

<root>/...dirPath.../dms.cfc?wsdl
<root>/...dirPath.../cobra.cfc?wsdl

<root>应该是动态的,因为应用程序用户必须设置此变量。

首先,我接受了

How can I dynamically switch web service addresses in .NET without a recompile?

并尝试了此

https://www.codeproject.com/Articles/12317/How-to-make-your-Web-Reference-proxy-URL-dynamic

我进一步找到了此链接

https://docs.microsoft.com/en-us/sql/reporting-services/report-server-web-service/net-framework/setting-the-url-property-of-the-web-service?view=sql-server-2017

,但是这些链接无济于事,我找不到设置URL behaviour,也无法通过代码访问URL属性。

我创建了一个静态类,该类应该处理两个Web服务。用户可以更改Web服务的根URL。

示例网址为

http://localhost:8500/CoBRA/...dirPath.../dms.cfc?wsdl

http://myInstance.com/CoBRA/...dirPath.../dms.cfc?wsdl

通过此代码处理

public static class CoBRAService
    {
        private static cobraClient cobraBaseClient = new cobraClient();

        private static dmsClient cobraDmsClient = new dmsClient();

        public static void SetWebserviceRootUrl(string rootUrl)
        {
            // cobraBaseClient.url = $"{rootUrl}/path/dms.cfc?wsdl";
            // cobraDmsClient.url = $"{rootUrl}/path/cobra.cfc?wsdl";
        }
    }

两个Web服务都不从System.Web.Services.Protocols.SoapHttpClientProtocol继承,它们实现了public partial class cobraClient : System.ServiceModel.ClientBase<MyProject.CoBRA_Base.cobra>, MyProject.CoBRA_Base.cobra

这是我的项目结构

enter image description here

我在哪里可以设置Web服务URL或如何访问url属性?

1 个答案:

答案 0 :(得分:1)

如果您的“ CoBRA_BaseClient”和“ CoBRA_DMSClient”是从 System.ServiceModel.ClientBase 继承的 那么您可以尝试以下操作:

public static CoBRA_BaseClient CreateService()
{
    CoBRA_BaseClient service = new CoBRA_BaseClient();
    service.Endpoint.Address = new EndpointAddress("uri");
    return service;
}

public static CoBRA_DMSClient CreateService()
{
    CoBRA_DMSClient service = new CoBRA_DMSClient();
    service.Endpoint.Address = new EndpointAddress("uri");
    return service;
}