WCF服务在调试中工作,但不作为Windows服务

时间:2012-12-07 03:47:48

标签: .net wcf wcf-binding self-hosting

我有一个自托管Windows服务的WCF服务。使用WCF测试客户端进行调试时,该服务非常有效。我使用Javascript和返回JSON的简单ajax请求。虽然当我将服务作为Windows服务运行时,请求会收到400错误。我猜这可能与我的配置有关。

感谢任何帮助。

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 个答案:

答案 0 :(得分: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>