我正在设置WCF服务和WPF应用程序。
该服务是一种双工服务,具有net.https
和net.tcp
绑定。也是https和TCP MEX端点。最后是udpDiscoveryEndpoint
,可以在网络中发现它。
应用程序将通过DiscoveryEndpoint
和UdpDiscoveryEndpoint
发现服务。之后,用ServiceEndpointCollection
确定MetadataResolver
。
我使用svcutil.exe创建合同并将其存储在DLL中。
重点是,我希望从服务中将强类型错误抛出给客户端,因此我也可以在客户端进行处理。
在我的第一次尝试中,我通过服务引用连接服务,设置FaultContract
,一切正常。
然后,我设置了一个类,以通过UdpDiscoveryEndpoint
发现应用,并解析元数据以获取端点。
这样,当执行以下代码时,我得到了KeyNotFoundException
:
MetadataResolver.Resolve(typeof(Contracts.ISvcRegistration), findResponse.Endpoints[0].Address)
当我从合同中删除FaultContractAttribute
时,它将起作用,但不会向客户端抛出强类型错误...
GetEndpoints:
// use a udp discovery client to discover the service
using (DiscoveryClient discoveryClient = new DiscoveryClient(new UdpDiscoveryEndpoint()))
{
FindCriteria findCriteria = null;
switch (serviceType)
{
case EService.SvcRegistration:
// find the metadata of the service type by MEX
findCriteria = FindCriteria.CreateMetadataExchangeEndpointCriteria(typeof(Contracts.ISvcRegistration));
break;
default:
break;
}
// only get one result
findCriteria.MaxResults = 1;
// find a service with the service type as criteria
var findResponse = discoveryClient.Find(findCriteria);
// null the criteria
findCriteria = null;
// getting the endpoint collection of the service
switch (serviceType)
{
case EService.SvcRegistration:
// exception will occur on the following line:
Endpoints_SvcRegistration = MetadataResolver.Resolve(typeof(Contracts.ISvcRegistration), findResponse.Endpoints[0].Address);
break;
default:
break;
}
// null the response
findResponse = null;
}
已下载合同中的摘录:
[System.ServiceModel.OperationContractAttribute(Action = "http://tempuri.org/ISvcRegistration/GetAccountAuthentication", ReplyAction = "http://tempuri.org/ISvcRegistration/GetAccountAuthenticationResponse")]
[System.ServiceModel.FaultContractAttribute(typeof(VisionHR_Dll.Services.Contracts.AccountNotMemberFault), Action = "http://tempuri.org/ISvcRegistration/GetAccountAuthenticationAccountNotMemberFault" +
"Fault", Name = "AccountNotMemberFault", Namespace = "http://schemas.datacontract.org/2004/07/VisionHR_Dll.Business.ExceptionObjects")]
在服务上:
[OperationContract]
[FaultContract(typeof(VisionHR_Dll.Business.ExceptionObjects.AccountNotMemberFault))]
bool GetAccountAuthentication(VisionHR_Dll.Auxiliaries.Security.AuthenticationWallet wallet, byte[] chiper);
当我移除FaultContractAttribute
时,它将起作用。