【问题标题】:generate webservice in Visual Studio with a dynamic root URL在 Visual Studio 中使用动态根 URL 生成 Web 服务
【发布时间】:2018-10-23 13:30:44
【问题描述】:

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

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

&lt;root&gt; 应该是动态的,因为应用程序用户必须设置此变量。

首先我拿了这个

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&lt;MyProject.CoBRA_Base.cobra&gt;, MyProject.CoBRA_Base.cobra

这是我的项目结构

在哪里可以设置 webservice url 或者如何访问 url 属性?

【问题讨论】:

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


    【解决方案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;
    }
    

    【讨论】:

    • 不抱歉。它们设置为public partial class cobraClient : System.ServiceModel.ClientBase&lt;MyProject.CoBRA_Base.cobra&gt;, MyProject.CoBRA_Base.cobra
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多