1

I have a WCF service that is a self hosted Windows Service. When debugging with the WCF Test Client the service works great. I use Javascript with simple ajax requests that return JSON. Although when I run the service as a Windows Service the request get a 400 error. I'm guessing this may have something to do with my config.

Any help is appreciated.

WCF Test Client Config

<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 Service 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 Service Implementation

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 Interface

    #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);
    }
4

1 回答 1

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>
于 2012-12-07T13:20:50.707 回答