WCF服务返回.svc的页面内容(内容类型text / html异常)

时间:2016-07-05 13:25:25

标签: c# wcf

我在本地开发了一个WCF服务器并对其进行了测试,并且工作正常(返回字符串“Hello World”)。它托管在IIS

但是当我将它部署到我在GoDaddy上托管的网站时,服务器会发送Service1.svc(描述内容)的页面内容,而不是客户端不期望也不会处理的“Hello World!”。

客户端配置:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="BasicHttpBinding_IMessage2" />
        </basicHttpBinding>
    </bindings>
    <client>
        <endpoint address="http://mywebaddress/myservice/Service1.svc"
            binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IMessage2"
            contract="ServiceReference2.IMessage" name="BasicHttpBinding_IMessage2" />
    </client>
</system.serviceModel>

服务器配置:

<?xml version="1.0"?>
<configuration>

<appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true"/>
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5.2"/>
<httpRuntime targetFramework="4.5.2"/>
<httpModules>
  <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web"/>
</httpModules>
</system.web>
<system.serviceModel>
<services>
  <service behaviorConfiguration="HelloServiceBehavior" name="HelloService.HelloService">
    <endpoint address="" binding="basicHttpBinding" bindingConfiguration=""
      contract="HelloService.IMessage" />
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="HelloServiceBehavior">
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="false" />
    </behavior>
  </serviceBehaviors>
</behaviors>
<protocolMapping>
    <add binding="basicHttpsBinding" scheme="https"/>
</protocolMapping>    
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
  <remove name="ApplicationInsightsWebTracking"/>
  <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web"
    preCondition="managedHandler"/>
</modules>
<!--
    To browse web app root directory during debugging, set the value below to true.
    Set to false before deployment to avoid disclosing web app folder information.
  -->
<directoryBrowse enabled="true"/>
<validation validateIntegratedModeConfiguration="false"/>
</system.webServer>

</configuration>

客户端在调用服务方法时崩溃:

Unhandled Exception: System.ServiceModel.ProtocolException: The content type text/html; charset=utf-8 of the response message doesn't match the content type of the binding

Fiddler上的服务响应,这是浏览器中www.XXXXX.com/Service.svc的确切内容:

 <HTML><HEAD><link rel="alternate" type="text/xml" href="http://XXXXXXX.net/myservice/Service1.svc?disco"/><STYLE type="text/css">#content{ FONT-SIZE: 0.7em; PADDING-BOTTOM: 2em; MARGIN-LEFT: 30px}BODY{MARGIN-TOP: 0px; MARGIN-LEFT: 0px; COLOR: #000000; FONT-FAMILY: Verdana; BACKGROUND-COLOR: white}P{MARGIN-TOP: 0px; MARGIN-BOTTOM: 12px; COLOR: #000000; FONT-FAMILY: Verdana}PRE{BORDER-RIGHT: #f0f0e0 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #f0f0e0 1px solid; MARGIN-TOP: -5px; PADDING-LEFT: 5px; FONT-SIZE: 1.2em; PADDING-BOTTOM: 5px; BORDER-LEFT: #f0f0e0 1px solid; PADDING-TOP: 5px; BORDER-BOTTOM: #f0f0e0 1px solid; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e5e5cc}.heading1{MARGIN-TOP: 0px; PADDING-LEFT: 15px; FONT-WEIGHT: normal; FONT-SIZE: 26px; MARGIN-BOTTOM: 0px; PADDING-BOTTOM: 3px; MARGIN-LEFT: -30px; WIDTH: 100%; COLOR: #ffffff; PADDING-TOP: 10px; FONT-FAMILY: Tahoma; BACKGROUND-COLOR: #003366}.intro{MARGIN-LEFT: -15px}</STYLE><TITLE>HelloService Service</TITLE></HEAD><BODY><DIV id="content"><P class="heading1">HelloService Service</P><BR/><P class="intro">You have created a service.<P class='intro'>To test this service, you will need to create a client and use it to call the service. You can do this using the svcutil.exe tool from the command line with the following syntax:</P> <BR/><PRE>svcutil.exe <A HREF="http://alqasm.net/myservice/Service1.svc?wsdl">http://alqasm.net/myservice/Service1.svc?wsdl</A></PRE><P>You can also access the service description as a single file:<BR/><PRE><A HREF="http://alqasm.net/myservice/Service1.svc?singleWsdl">http://alqasm.net/myservice/Service1.svc?singleWsdl</A></PRE></P></P><P class="intro"/>This will generate a configuration file and a code file that contains the client class. Add the two files to your client application and use the generated client class to call the Service. For example:<BR/><P class='intro'><B>C#</B></P><PRE><font color="blue">class </font><font color="teal">Test
</font>{
<font color="blue">    static void </font>Main()
    {
        <font color="teal">MessageClient</font> client = <font color="blue">new </font><font color="teal">MessageClient</font>();

<font color="green">        // Use the 'client' variable to call operations on the service.

</font><font color="green">        // Always close the client.
</font>        client.Close();
    }
}
</PRE><BR/><P class='intro'><B>Visual Basic</B></P><PRE><font color="blue">Class </font><font color="teal">Test
</font><font color="blue">    Shared Sub </font>Main()
<font color="blue">        Dim </font>client As <font color="teal">MessageClient</font> = <font color="blue">New </font><font color="teal">MessageClient</font>()
<font color="green">        ' Use the 'client' variable to call operations on the service.

</font><font color="green">        ' Always close the client.
</font>        client.Close()
<font color="blue">    End Sub
</font><font color="blue">End Class</font></PRE></DIV></BODY></HTML>

非常感谢您的支持

1 个答案:

答案 0 :(得分:0)

您需要在IIS中为.SVC添加MIME类型,或者为了方便修复使用,可以在IIS中注册asp.net。见链接msdn.microsoft.com/en-IN/library/k6h9cz8h(v=vs.100).aspx。