更多WCF问题...... :)
我的所有工作流程都实现了相同的3种方法.经过大量的复制和粘贴后,我决定让它们从相同的界面继承:
[ServiceContract(Namespace = "http://schema.company.com/messages/")]
public interface IBasicContract<TRequest, TResponse>
where TRequest : class
where TResponse : class
{
[OperationContract(Name = "GetReport",
Action = "http://schema.company.com/messages/GetReport",
ReplyAction = "http://schema.company.com/messages/GetReportResponse")]
TResponse GetReport(TRequest inquiry);
[OperationContract(Name = "GetRawReport",
Action = "http://schema.company.com/messages/GetRawReport",
ReplyAction = "http://schema.company.com/messages/GetRawReportResponse")]
string GetRawReport(string guid);
[OperationContract(Name = "GetArchiveReport",
Action = "http://schema.company.com/messages/GetArchiveReport",
ReplyAction = "http://schema.company.com/messages/GetArchiveReportResponse")]
TResponse GetArchiveReport(string guid);
}
Run Code Online (Sandbox Code Playgroud)
我还决定创建服务客户端的通用实现:
public class BasicSvcClient<TRequest, TResponse> : ClientBase<IBasicContract<TRequest, TResponse>>, IBasicContract<TRequest, TResponse>
where TRequest : class
where TResponse : class
{
public BasicSvcClient()
{
}
public BasicSvcClient(string endpointConfigurationName) :
base(endpointConfigurationName)
{
}
public BasicSvcClient(string endpointConfigurationName, string remoteAddress) :
base(endpointConfigurationName, remoteAddress)
{
}
public BasicSvcClient(string endpointConfigurationName, EndpointAddress remoteAddress) :
base(endpointConfigurationName, remoteAddress)
{
}
public BasicSvcClient(Binding binding, EndpointAddress remoteAddress) :
base(binding, remoteAddress)
{
}
public TResponse GetReport(TRequest inquiry)
{
return Channel.GetReport(inquiry);
}
public string GetRawReport(string guid)
{
return Channel.GetRawReport(guid);
}
public TResponse GetArchiveReport(string guid)
{
return Channel.GetArchiveReport(guid);
}
}
Run Code Online (Sandbox Code Playgroud)
问题是当我尝试使用它时:
using (var client = new BasicSvcClient<TRequest, TResponse>())
{
var response = client.GetReport(inquiry);
context.Response.ContentType = "text/xml";
context.Response.Write(response.AsXML());
}
Run Code Online (Sandbox Code Playgroud)
我总是得到一个错误,说它无法找到合同IBasicContract的配置,因为.NET使用的奇怪语法:
找不到引用合同'BasicWorkflow.IBasicContract`2的默认端点元素...
我试过这样做:
using (var client = new BasicSvcClient<TRequest, TResponse>("myConfig"))
Run Code Online (Sandbox Code Playgroud)
它没有帮助 - 它仍然在寻找特定的合同.
我知道ServiceContract属性有一个ConfigurationName参数,但我不能在编译时使用它,因为我有很多工作流程,我从同一个程序调用(因此有很多配置项).有没有办法在运行时设置ConfigurationName?我认为这是ClientBase构造函数应该做的,但显然不是.
[编辑]这是.config文件中的端点,我不相信它在这种情况下非常有用:
<endpoint address="https://localhost/services/Contract.svc"
binding="basicHttpBinding"
bindingConfiguration="httpsDataEndpoint"
contract="IContract" name="IContractSvc" />
Run Code Online (Sandbox Code Playgroud)
[编辑2]好的......我发现了一种有效的方法,尽管我仍然不满意它:
using (var wf = new BasicSvcClient<TRequest, TResponse>(
new BasicHttpBinding("httpsDataEndpoint"),
new EndpointAddress("https://localhost/services/Contract.svc")))
Run Code Online (Sandbox Code Playgroud)
我现在唯一的问题是我更愿意从.config文件中检索端点地址(使用实际的合同名称,如IContract).有谁可以帮我这个部分?
[Edit3]终于找到了完整的解决方案:)万岁反射器!
var cf = (ClientSection) ConfigurationManager.GetSection("system.serviceModel/client");
foreach (ChannelEndpointElement endpoint in cf.Endpoints)
{
if (endpoint.Name != "ContractSvc")
continue;
using (var wf = new BasicSvcClient<TRequest, TResponse>(
new BasicHttpBinding("httpsDataEndpoint"),
new EndpointAddress(endpoint.Address.ToString())))
{
//... call wf.GetReport()
}
break;
}
Run Code Online (Sandbox Code Playgroud)
“.NET 使用的奇怪语法”实际上是运行时绑定到特定类型的泛型类型的类型名称。Typename`n[[Type],...] 其中 n 表示泛型类型中包含的类型参数的数量。
那么您的端点配置是什么样的?