WCF在root(/)目录中自托管soap-service

时间:2014-02-28 14:15:29

标签: c# web-services wcf soap self-hosting

我有设备(充电器)将SOAP消息发送到IP I我将收到状态报告,但它总是在根目录中发送,并且不可更改:http://192.168.1.2/

我自己托管了一个WCF服务,我希望收到这些消息。但我找不到任何有关网址重写或收听其他内容的信息,而不是服务名称,在我的情况下,它变为http://192.168.1.2/ocpp15

是否可以以任何方式从/ to / ocpp15 /或从/ ocpp15 /重写/路由到/?

在Windows服务中托管

        var smb = new ServiceMetadataBehavior
        {
            HttpGetEnabled = true,
            MetadataExporter = { PolicyVersion = PolicyVersion.Policy12 },
        };
        var serviceDebugBehavior = new ServiceDebugBehavior { IncludeExceptionDetailInFaults = true };

       var baseAddress15 = new Uri(String.Format("http://{0}/, Environment.MachineName));
        var host15 = new ServiceHost(typeof(Ocpp15), baseAddress15);

        host15.Description.Behaviors.Add(smb);
        host15.Description.Behaviors.Remove(typeof(ServiceDebugBehavior));
        host15.Description.Behaviors.Add(serviceDebugBehavior);
        host15.AddServiceEndpoint(ServiceMetadataBehavior.MexContractName, MetadataExchangeBindings.CreateMexHttpBinding(), "mex");
        host15.AddServiceEndpoint(typeof(IOcpp15), new WSHttpBinding(SecurityMode.None), "");
        host15.Open();

服务联系

使用/ sc选项

从WSDL文件生成
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
[System.ServiceModel.ServiceContractAttribute(Namespace = "urn://Ocpp/Cs/2012/06/", ConfigurationName = "Ocpp15")]
public interface IOcpp15
{
    // CODEGEN: Generating message contract since the wrapper name (authorizeRequest) of message AuthorizeRequest does not match the default value (Authorize)
    [System.ServiceModel.OperationContractAttribute(Action = "/Authorize", ReplyAction = "/AuthorizeResponse")]
    [System.ServiceModel.XmlSerializerFormatAttribute()]
    AuthorizeResponse Authorize(AuthorizeRequest request);
    and so on.....

服务

public class Ocpp15 : IOcpp15
{
    public AuthorizeResponse Authorize(AuthorizeRequest request)
    {
        return new AuthorizeResponse { idTagInfo = ValidateRequest(request) };
    }
    and so on.....

1 个答案:

答案 0 :(得分:0)

我在WCF中没有找到任何解决方案,但是使用Fiddler进行黑客攻击。 设置fiddler以侦听端口80并接受外部请求。在另一个端口上收听WCF,在我的例子中是8081。

将此过滤器添加到OnBeforeRequest:

if (oSession.host.toLowerCase() == "192.168.1.2" || oSession.host.toLowerCase() ==          "192.168.1.2:80") 
{
     oSession.host = "192.168.1.2:8081";

     if (oSession.PathAndQuery=="/") 
     {
        oSession.PathAndQuery="/ocpp15/";

        var strBody1 = oSession.GetRequestBodyAsString ();
        strBody1 = strBody1.replace("http://172.28.0.30/","http://172.28.0.30:8081/ocpp15/");
        oSession.utilSetRequestBody (strBody1);
     }
}

最后一个身体替换是有时使用的soap To字段。