我正在azure中部署一个包含web站点和wcf服务的webrole ...
该站点使用来自wcf的服务.
这里的问题是,登台部署为端点创建了一个疯狂的URL,我必须不断更改web.config中的端点...
我想知道是否有办法要么"预测"网址是什么,要么强迫一个甚至指向一个通用主机,如"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
Run Code Online (Sandbox Code Playgroud)
在派生类中,我向它传递以下变量(例如):
this.roleName = "WebServiceRole";
this.endpointName = "HttpInternal";
this.serviceName = "services/Accounts.svc";
Run Code Online (Sandbox Code Playgroud)
我根本不需要参考暂存(或生产)URL。
有关更多详细信息,请参阅我的答案:在同一解决方案中添加 WCF 引用,而不添加服务引用
| 归档时间: |
|
| 查看次数: |
250 次 |
| 最近记录: |