我是否可以从路由服务

时间:2015-06-17 14:53:02

标签: c# .net web-services wcf encryption

我公司长期以来一直通过路由程序使用ASPX和Windows服务来管理连接,并允许我们的数据中心控制客户端连接的位置。

就在最近我们开始使用MVC和WCF。是的,在2015年,我们正在转向这些事情。无论如何,他们在发布前一个月就想出了他们需要路由WCF流量,因为它位于防火墙外或DMZ中。

我们将把路由服务放在网络中并在防火墙上打开一个端口,这样所有服务都可以通过路由服务上的防火墙进行通信保护。

我提供所有这些细节的唯一原因是因为这确定了我们如何配置WCF服务的期望。我们使用证书进行安全性和邮件加密,因为我们必须加密邮件。我们还添加了一个自定义标头,我们的路由器将用它来路由消息。

我们的工作是路由器接收消息,检查标头,查找是否有端点,转发消息,获取响应并将其发送回WCF服务。目前这一切都很美妙。我们甚至可以从端点接收消息故障并将其发送回客户端。

我正在努力解决的问题是,当路由器出现故障时,路由器如何将messageFault发送回客户端。但是因为我的路由器不知道如何加密消息,所以这个故障是未加密的。这些错误就像"终点无法到达"或者"没有找到端点"。客户端收到以下错误:

An unsecured or incorrectly secured fault was received from the other party. 
See the inner FaultException for the fault code and detail.

内部异常是我在下面的代码中定义的内容。

Dim fe As FaultException = New FaultException(status)
Dim fault As MessageFault = fe.CreateMessageFault()
Dim msg = System.ServiceModel.Channels.Message.CreateMessage(getMessageVersion, fault, getAction)

getMessageVersion将根据客户端返回Soap11或Soap12 getAction将从标头中的客户端请求返回操作。

有没有办法配置客户端接受这些错误,即使它们没有加密?我们希望将它们作为故障异常而不是异常来捕获它们,以便将故障异常逻辑保持在一起用于一般故障。

任何帮助或见解将不胜感激。我们的新程序正在使用C#,我们的旧程序仍然是用VB编写的,所以抛弃任何.Net代码给我,你很满意,我会使用它。

2 个答案:

答案 0 :(得分:1)

以下是Fault类:

    [Serializable]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "yourNamespace")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "yourNamespace")]
[System.Runtime.Serialization.DataContractAttribute(Namespace = "yourNamespace")]
public class CustomFault : ISerializable
{
    public string Message { get; set; }
}

以下是引用Fault的接口和操作方法:

[ServiceContract(Namespace = "yourNamespace")]
public interface IService
{
    [FaultContractAttribute(
        typeof(CustomFault),
        Action = "", 
        Name = "Fault", 
        Namespace = "yourNamespace")]
    [System.ServiceModel.XmlSerializerFormatAttribute(SupportFaults = true)]
    [OperationContract]
    Response Operation(Request request);
}

最后我如何抛出错误:

throw new FaultException<CustomFault>
             (
                 new GuiaMedicoFault("Custom Error description"),
                 new FaultReason("Error description")
             );

也许这部分代码可以帮助您或提供任何想法。

答案 1 :(得分:0)

利用里卡多的信息,我能够缩小我的工作范围。对此的实际答案完全取决于端点配置。这取决于你的绑定。

我们没有必要改变路由器中的任何东西,但我们不得不改变它看起来像这样的客户端。

    serviceClient.Endpoint.Binding = getWSHttpBinding();

那应该是

    private CustomBinding getWSHttpBinding()
    {
        WSHttpBinding binding = new WSHttpBinding();
        binding.Security.Mode = SecurityMode.Message;
        binding.Security.Message.EstablishSecurityContext = false;
        binding.Security.Message.NegotiateServiceCredential = false;
        binding.Security.Message.ClientCredentialType = MessageCredentialType.Certificate;
        binding.MaxReceivedMessageSize = (1024 * 1024 * 10);
        BindingElementCollection elements = binding.CreateBindingElements();
        elements.Find<SecurityBindingElement>().EnableUnsecuredResponse = true;

        return new CustomBinding(elements);
    }

如果您发现此问题,此链接会提供更多信息。 https://support.microsoft.com/en-us/kb/971493

我们可能只是将此作为例外处理而不是故障。

相关问题