2012-07-31 50 views
2

我正在使用Castle WCF集成工具,并且我已经为我的第一个webHttp端点正常工作。要使此端点起作用,它需要端点启用WebHttpBehavior。我能做到这一点使用:指定城堡WCF集成工具每个端点的端点行为

  container.Register(Component.For<IEndpointBehavior>() 
          .ImplementedBy<WebHttpBehavior>()); 

这将成为一个问题,当我尝试启用使用basicHttpBinding的第二端点这是不符合的WebHttpBehavior兼容。

有没有办法指定上面的IEndPointBehavior注册只适用于某个端点?

这是我的服务完整的安装程序:

  container.AddFacility<WcfFacility>(f => f.CloseTimeout = TimeSpan.Zero) 
       .Register(Component.For<IDiagnosticService>() 
        .ImplementedBy<DiagnosticService>() 
        .Named("DiagnosticService") 
        .LifestyleTransient() 
        .AsWcfService(new DefaultServiceModel() 
            .Hosted() 
            .AddEndpoints(WcfEndpoint.BoundTo(new WebHttpBinding()).At("json")) 
            .AddEndpoints(WcfEndpoint.BoundTo(new BasicHttpBinding()).At("soap")) 
            .PublishMetadata(o => o.EnableHttpGet()))); 


      container.Register(Component.For<IEndpointBehavior>() 
          .ImplementedBy<WebHttpBehavior>()); 
+0

的WcfEndpoint类接受System.ServiceModel.ServiceEndpoint的一个实例。使用这个我可以根据需要独立配置端点。这似乎工作正常,但我无法弄清楚如何处理相对寻址(即(http://local/ser.svc/json vs(http://local/ser.svc/soap) )的构造函数System.ServiceModel.EndpointAddress需要一个URI,如果我把整个uri(http://local/ser.svc/json“),我得到一个”无协议绑定匹配“异常如果我只使用http:// local /ser.svc作为URI,它可以工作,但我没有/ json和/ soap端点地址。 – Bluffrock 2012-07-31 23:39:09

回答

3

确定。我终于明白了这一点。事实证明,我的问题大部分都与Azure仿真环境有关,而不是Castle WCF集成。答案非常简单 - 只需设置ServiceEndpoint实例并使用WcfEndpoint.FromEndpoint()方法即可。

这是我工作的安装程序:

 String internalEndpointAddress = string.Format("http://{0}/DiagnosticService.svc", 
              RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["Endpoint1"].IPEndpoint); 

     // This ContractDescription instance must be used for both endpoints in this case 
     ContractDescription description = ContractDescription.GetContract(typeof(IDiagnosticService)); 

     // Create JSON webHTTP Binding    
     WebHttpBinding webhttpbinding = new WebHttpBinding(); 
     string jsonURI = internalEndpointAddress + "/json"; 
     EndpointAddress jsonEndpointAddress = new EndpointAddress(new Uri(jsonURI)); 
     ServiceEndpoint jsonEndpoint = new ServiceEndpoint(description, webhttpbinding, jsonEndpointAddress); 
     jsonEndpoint.Behaviors.Add(new WebHttpBehavior()); 


     // Create WSHTTP Binding   
     WSHttpBinding wsHttpBinding = new WSHttpBinding(); 
     string soapURI = internalEndpointAddress + "/soap"; 
     EndpointAddress soapEndpointAddress = new EndpointAddress(new Uri(soapURI)); 
     ServiceEndpoint soapEndpoint = new ServiceEndpoint(description, wsHttpBinding, soapEndpointAddress); 



     container.AddFacility<WcfFacility>(f => f.CloseTimeout = TimeSpan.Zero) 
       .Register(Component.For<IDiagnosticService>() 
        .ImplementedBy<DiagnosticService>() 
        .Named("DiagnosticService") 
        .LifestyleTransient() 
        .AsWcfService(new DefaultServiceModel() 
            .Hosted() 
            .AddEndpoints(WcfEndpoint.FromEndpoint(jsonEndpoint)) 
            .AddEndpoints(WcfEndpoint.FromEndpoint(soapEndpoint)) 
            .PublishMetadata(o => o.EnableHttpGet())));