我正在 azure 中部署一个 webrole,其中包含一个网站和一个 wcf 服务...
该站点使用来自 wcf 的服务。
这里的问题是登台部署为端点创建了一个疯狂的 url,我必须不断更改 web.config 中的端点......
我想知道是否有一种方法可以“预测” url 将是什么,或者强制一个甚至指向一个通用主机,例如“localhost”?
您应该能够使用角色发现来查找 WCF 终结点。请在此处查看此 SO 答案及其链接到的博客文章。
我自己用于连接到 azure 服务的抽象基类就是基于那篇文章。它使用角色发现来创建一个通道,如下所示:
#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。
有关更多详细信息,请参阅我的答案:在同一解决方案中添加 WCF 引用而不添加服务引用
无法预测、控制 GUID 或使用某些常量名称。
为了使事情更容易,您可以做的是将 URL 移动到 .CSCFG 并从 Azure 管理门户更新 WCF 服务的 URL