2012-12-07 73 views
1

我有一个自我托管的Windows服务的WCF服务。使用WCF Test Client进行调试时,该服务非常有用。我使用JavaScript与返回JSON的简单Ajax请求。尽管当我将该服务作为Windows服务运行时,请求会收到400错误。我猜这可能与我的配置有关。WCF服务在调试中起作用,但不作为Windows服务工作

任何帮助表示赞赏。

WCF测试客户端配置

<system.serviceModel> 
    <bindings> 
     <wsHttpBinding> 
      <binding name="MetadataExchangeHttpBinding_ISkyMobileService"> 
       <security mode="None" /> 
      </binding> 
     </wsHttpBinding> 
    </bindings> 
    <client> 
     <endpoint address="http://localhost:8523/HLT/Sky/SkyMobileService/mex" 
      binding="wsHttpBinding" bindingConfiguration="MetadataExchangeHttpBinding_ISkyMobileService" 
      contract="ISkyMobileService" name="MetadataExchangeHttpBinding_ISkyMobileService" /> 
    </client> 
</system.serviceModel> 

Windows服务App.Config中

<system.serviceModel> 
    <services> 
     <service name="HLT.Sky.MobileDeviceServices.SkyMobileService" behaviorConfiguration="HeliosMobileServiceBehavior"> 
     <host> 
      <baseAddresses> 
      <add baseAddress="http://localhost:8523/HLT/Sky/SkyMobileService" /> 
      </baseAddresses> 
     </host> 
     <endpoint address="" binding="webHttpBinding" contract="HLT.Sky.MobileDeviceServices.ISkyMobileService" bindingNamespace="http://HLT.Sky.MobileDeviceServices" /> 
     <endpoint address="mex" binding="mexHttpBinding" contract="HLT.Sky.MobileDeviceServices.ISkyMobileService" bindingNamespace="http://HLT.Sky.MobileDeviceServices" /> 
     </service> 
    </services> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior name="SkyMobileServiceBehavior"> 
      <serviceMetadata httpGetEnabled="true"/> 
      <serviceDebug includeExceptionDetailInFaults="false"/> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    </system.serviceModel> 

Windows服务实现

using System.ServiceProcess; 
using System.ServiceModel; 


namespace HLT.Sky.WindowsService1 
{ 
    public partial class Service1 : ServiceBase 
    { 
     internal static ServiceHost myServiceHost = null; 

     public Service1() 
     { 
      InitializeComponent(); 
     } 

     protected override void OnStart(string[] args) 
     { 
      if (myServiceHost != null) 
      { 
       myServiceHost.Close(); 
      } 
      myServiceHost = new ServiceHost(typeof(MobileDeviceServices.SkyMobileService)); 
      myServiceHost.Open(); 
     } 

     protected override void OnStop() 
     { 
      if (myServiceHost != null) 
      { 
       myServiceHost.Close(); 
       myServiceHost = null; 
      } 
     } 
    } 
} 

WCF接口

#region GET 
    // Return JSON Store for specified chartType 
    [OperationContract] 
    [WebGet(UriTemplate = "GetChartData?chartType={chartType}&serialNumber={serialNumber}&_dc={dc}&limit={limit}&callback={callback}", ResponseFormat = WebMessageFormat.Json)] 
    string GetChartData(string dc, string limit, string callback, int chartType, string serialNumber); 

    [OperationContract] 
    [WebGet(ResponseFormat = WebMessageFormat.Json)] 
    string GetHomePageData(); 
    #endregion 

    #region POST 
    [OperationContract] 
    [WebInvoke(Method = "POST", UriTemplate = "VerifyPINData?pinData={pinData}", ResponseFormat = WebMessageFormat.Json)] 
    bool VerifyPINData(string pinData); 

    [OperationContract] 
    [WebInvoke(Method = "POST", UriTemplate = "SubmitCNPData?cnpData={cnpData}&serialNumber={serialNumber}", ResponseFormat = WebMessageFormat.Json)] 
    bool SubmitCNPData(int cnpData, string serialNumber); 

    [OperationContract] 
    [WebInvoke(Method = "POST", UriTemplate = "SendEmail?mailTo={mailTo}&subject={subject}&message={message}",ResponseFormat = WebMessageFormat.Json)] 
    bool SendEmail(string mailTo, string subject, string message); 

    #endregion 

Program.cs的

static void Main() 
    { 
     ServiceBase[] ServicesToRun; 
     ServicesToRun = new ServiceBase[] 
     { 
      new Service1() 
     }; 
     ServiceBase.Run(ServicesToRun); 
    } 
+1

您能描述一下如何让您的wcf在Windows服务中运行。您必须在Windows服务类中实现启动方法,并将您自己托管的wcf设置为 – Dmitry

+0

尝试使用Fidler进行故障排除。 – abatishchev

+0

也尝试在IIS中托管而不是自托管Windows服务。 – abatishchev

回答

1

添加的终结点行为,并删除 “MEX” 端点最终解决了这个问题。请参阅下面的工作配置文件:

<system.serviceModel> 
    <client> 
     <endpoint binding="webHttpBinding" bindingConfiguration="" contract="HLT.Sky.MobileDeviceServices.ISkyMobileService" /> 
    </client> 
    <services> 
     <service name="HLT.Sky.MobileDeviceServices.SkyMobileService" behaviorConfiguration="SkyMobileServiceBehavior"> 
     <host> 
      <baseAddresses> 
      <add baseAddress="http://localhost:8523/HLT/Sky/SkyMobileService" /> 
      </baseAddresses> 
     </host> 
     <endpoint address="" binding="webHttpBinding" contract="HLT.Sky.MobileDeviceServices.ISkyMobileService" /> 
     </service> 
    </services> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior name="SkyMobileServiceBehavior"> 
      <serviceMetadata httpGetEnabled="true"/> 
      <serviceDebug includeExceptionDetailInFaults="true"/> 
     </behavior> 
     </serviceBehaviors> 
      <endpointBehaviors> 
     <behavior> 
      <webHttp/> 
     </behavior> 
     </endpointBehaviors> 
    </behaviors> 
    </system.serviceModel> 
+0

这很好,你解决了这个问题,但你说:_“尽管当我将服务作为Windows服务运行时,请求会得到400错误,我猜测”_“。你不应该猜测,你应该查找错误并修复它。如果没有mex端点,你将不得不退回到WSDL,这并不总是理想的选择。 – CodeCaster

+0

@CodeCaster好点。我知道它现在需要配置我的配置。我只知道何时添加mex端点,我得到的HTTP状态为200,但没有数据返回。当我删除它,然后我得到的数据。我不完全确定这是为什么。 – fortune