我正在 azure 中部署一个 webrole,其中包含一个网站和一个 wcf 服务...
该站点使用 wcf 的服务。
这里的问题是,暂存部署为端点创建了一个疯狂的 url,我必须不断更改 web.config 中的端点...
我想知道是否有一种方法可以“预测”网址是什么,或者强制一个或什至指向一个通用主机,例如“localhost”???
最佳答案
您应该能够使用角色发现来查找 WCF 端点。请参阅此答案 here和 blog post它链接到。
我自己的用于连接到 azure 服务的抽象基类基于该文章。它使用角色发现来创建这样的 channel :
#region Channel
protected String roleName;
protected String serviceName;
protected String endpointName;
protected String protocol = @"http";
protected EndpointAddress _endpointAddress;
protected BasicHttpBinding httpBinding;
protected NetTcpBinding tcpBinding;
protected IChannelFactory channelFactory;
protected T client;
protected virtual AddressHeader[] addressHeaders
{
get
{
return null;
}
}
protected virtual EndpointAddress endpointAddress
{
get
{
if (_endpointAddress == null)
{
var endpoints = RoleEnvironment.Roles[roleName].Instances.Select(i => i.InstanceEndpoints[endpointName]).ToArray();
var endpointIP = endpoints.FirstOrDefault().IPEndpoint;
if(addressHeaders != null)
{
_endpointAddress = new EndpointAddress(new Uri(String.Format("{1}://{0}/{2}", endpointIP, protocol, serviceName)), addressHeaders);
}
else
{
_endpointAddress = new EndpointAddress(String.Format("{1}://{0}/{2}", endpointIP, protocol, serviceName));
}
}
return _endpointAddress;
}
}
protected virtual Binding binding
{
get
{
switch (protocol)
{
case "tcp.ip":
if (tcpBinding == null) tcpBinding = new NetTcpBinding();
return tcpBinding;
default:
//http
if (httpBinding == null) httpBinding = new BasicHttpBinding();
return httpBinding;
}
}
}
public virtual T Client
{
get
{
if (this.client == null)
{
this.channelFactory = new ChannelFactory<T>(binding, endpointAddress);
this.client = ((ChannelFactory<T>)channelFactory).CreateChannel();
((IContextChannel)client).OperationTimeout = TimeSpan.FromMinutes(2);
var scope = new OperationContextScope(((IContextChannel)client));
addCustomMessageHeaders(scope);
}
return this.client;
}
}
#endregion
在派生类中,我向它传递以下变量(例如):
this.roleName = "WebServiceRole";
this.endpointName = "HttpInternal";
this.serviceName = "services/Accounts.svc";
我根本不需要引用暂存(或生产)URL。
请参阅我的回答以了解更多详细信息:Add WCF reference within the same solution without adding a service reference
关于wcf - 处理 Azure 暂存疯狂 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15091178/