0

我有以下代码在 Visual Studio 2010 WCF 项目中工作。
但是,当我在 Visual Studio 2008 中重新创建项目时,它没有工作,只给了我以下错误 StatuesText“不支持的媒体类型”。
编辑:
我发现问题出在 Web.config 文件中,因为它在 Visual Studio 2010 项目和 2008 项目之间有所不同。因此,我已将所有 Web.config 添加到问题中。

使用soap调用WCF服务的javascript如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Untitled Page</title>
    <script type="text/javascript" src="jQuery/jquery-1.3.2.min.js"></script>
    <script type="text/javascript" src="jQuery/jquery.xml2json.js"></script>
    <script type="text/javascript" src="jQuery/json2.js"></script>
    <script type="text/javascript">
        function MyRequest() {
            debugger;
            var methodName = "GetData"
            var responseTagName = methodName + "Response";
            var resultTagName = methodName + "Result";
            var sendData = "";
            sendData += '<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">';
            sendData += ' <s:Body>';
            sendData += ' <' + methodName + ' xmlns="http://tempuri.org/">';
            sendData += ' <name>Shlomy</name>';
            sendData += ' </' + methodName + '>';
            sendData += ' </s:Body>';
            sendData += '</s:Envelope>';
            var response = $.ajax({
                type: "POST",
                contentType: "text/xml; charset=utf-8",
                methodName: methodName,
                url: "http://localhost:16109/Service1.svc",
                data: sendData,
                async: false,
                beforeSend: function (req, methodName) {
                    var soapActionURI = "http://tempuri.org/IService1/GetData";
                    req.setRequestHeader("SOAPAction", soapActionURI);
                },
                success: function (xml, type, xhr) {
                    var result = $(xml).find(resultTagName);
                    var resultjson = $.xml2json(result);
                    successHandler(xml.text);
                },
                error: function (xmlHttpRequest, textStatus, errorThrown) {
                    alert(xmlHttpRequest.responseText);
                }
            });
        }

        function successHandler(soapResult) {
            alert(soapResult);
        }
    </script>
</head>
<body>
    <input type="button" onclick="MyRequest()" value="Call WCF via AJAX" />
</body>
</html>  

WCF 服务(HelloWorldWCFService)如下:
Service1.svc:

<%@ ServiceHost Language="C#" Debug="true" Service="HelloWorldWCFServiceLibrary.Service1" %>
<%@ Assembly Name="HelloWorldWCFServiceLibrary" %>    

WCF 网络配置:

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

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="debug">
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service name="HelloWorldWCFServiceLibrary.Service1" behaviorConfiguration="debug" >
        <endpoint address="mex" binding="wsHttpBinding" contract="HelloWorldWCFServiceLibrary.IService1" />
      </service>
    </services>
  </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

</configuration>

WCF 库(HelloWorldWCFServiceLibrary)如下:
IService.cs:

    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        CompositeType GetDataUsingDataContract(CompositeType composite);

        // TODO: Add your service operations here
        [OperationContract]
        string GetData(string name);
    }

服务1.cs:

public class Service1 : IService1
{
    public string GetData(string name)
    {
        return "WCFSoapTest";
    }
}

我已经尝试了几天来弄清楚 2008 解决方案和 2010 之间的区别以及为什么只有 2010 项目有效。但无济于事。有什么建议么 ?

谢谢。

4

1 回答 1

1

在挖掘了有关它的网络后,我能够让它工作。
原因和我想的一样——Web.config 文件
不同的是,在 Visual Studio 2010 中项目是用 .Net4.0 创建的,而 Visual Studio 2008 是用 .Net3.5 创建项目
的,生成的 Web.config 文件是 . Net4.0 与 .Net3.5 中生成的不同
我需要向服务添加一个带有“basicHttpBinding”的“端点”,以便 WCF 服务与 SOAP 一起工作。

<services>
   <service name="HelloWorldWCFServiceLibrary.Service1" behaviorConfiguration="debug2">
        <endpoint address="" binding="basicHttpBinding" contract="HelloWorldWCFServiceLibrary.IService1"/>
   </service>
</services>

由于此链接
,我找到了此信息 我的新 Web.config 文件如下:

    <?xml version="1.0"?>
<configuration>
  <configSections>
    <sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
        <section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
        <sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
          <section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="Everywhere"/>
          <section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
          <section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
          <section name="roleService" type="System.Web.Configuration.ScriptingRoleServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
        </sectionGroup>
      </sectionGroup>
    </sectionGroup>
  </configSections>
  <system.web>
    <compilation debug="true">
      <assemblies>
        <add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
        <add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add assembly="System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
        <add assembly="System.Data.DataSetExtensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
      </assemblies>
    </compilation>
    <pages>
      <controls>
        <add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add tagPrefix="asp" namespace="System.Web.UI.WebControls" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
      </controls>
    </pages>
    <httpHandlers>
      <remove verb="*" path="*.asmx"/>
      <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
      <add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
      <add verb="GET,HEAD" path="ScriptResource.axd" validate="false" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    </httpHandlers>
    <httpModules>
      <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    </httpModules>
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="debug2">
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
      <!--<endpointBehaviors>
                <behavior name="HelloWorldWCFService.AjaxAspNetAjaxBehavior"/>
            </endpointBehaviors>-->
    </behaviors>
    <!--<serviceHostingEnvironment aspNetCompatibilityEnabled="false"/>-->
    <services>
      <service name="HelloWorldWCFServiceLibrary.Service1" behaviorConfiguration="debug2">
        <!--<endpoint address="" binding="wsHttpBinding" contract="HelloWorldWCFServiceLibrary.IService1"/>-->
        <endpoint address="" binding="basicHttpBinding" contract="HelloWorldWCFServiceLibrary.IService1"/>
      </service>
    </services>
  </system.serviceModel>
  </configuration>

感谢任何试图提供帮助的人!

于 2012-10-10T16:43:12.450 回答