如何使您的服务参考代理URL动态化?

Ela*_*nda 4 c# wcf wcf-client

我有一个Web服务的Web引用:

using (var client = new GetTemplateParamSoapClient("GetTemplateParamSoap"))
{
    TemplateParamsKeyValue[] responsArray = client.GetTemplatesParamsPerId(
        CtId, tempalteIds.ToArray());

    foreach (var pair in responsArray)
    {
        string value = FetchTemplateValue(pair.Key, pair.Value);
        TemplateComponentsData.Add(pair.Key, value);
    }
}
Run Code Online (Sandbox Code Playgroud)

试图从c#代码更改Web引用URL:作为建议:

1)http://www.codeproject.com/KB/XML/wsdldynamicurl.aspx

2)如何使用可配置的URL调用Web服务

3)http://aspalliance.com/283_Setting_Web_Service_References_Dynamically

但是当我尝试做的时候,我发现符号丢失了:

client.Url
Run Code Online (Sandbox Code Playgroud)

另外我找不到"Url_behavior"的属性

lad*_*dge 6

听起来您已经添加了服务引用,但这里是添加,更新和删除服务引用的演练.

一旦你在项目中获得了其中一个,就可以使用其中一个构造函数重载来改变端点URI,正如John Saunders上面所说的那样.为此,您需要知道配置文件中端点的名称.例如,在添加服务后,您的配置文件中可能包含以下元素:

<endpoint address="http://bleh.com/services/servicename.asmx"
    binding="basicHttpBinding" bindingConfiguration="ServiceNameSoap"
    contract="ServiceReference1.ServiceNameSoap" name="ServiceNameSoap" />
Run Code Online (Sandbox Code Playgroud)

给定该端点,您可以address使用以下重载更改运行时:

var proxy = new ServiceReference1.ServiceNameSoapClient("ServiceNameSoap",
    "http://new-address.com/services/servicename.asmx");
Run Code Online (Sandbox Code Playgroud)

您也可以在施工后进行,但这会变得有点困难.如果需要,请参阅有关Endpoint属性和关联类型ServiceEndpoint的文档.