2012-10-09 183 views
0

我有以下代码可以在Visual Studio 2010 WCF项目中使用。
但是,当我在Visual Studio 2008中重新创建项目时,它没有奏效,只给了我下面的错误StatuesText“Unsupported Media Type”。
编辑:
我发现问题出在Web.config文件中,因为它与Visual Studio 2010项目和2008项目不同。因此我已将所有Web.config添加到问题中。Javascript + SOAP + WCF适用于Visual Studio 2010,但不适用于Visual Studio 2008

的JavaScript调用该用肥皂WCF服务如下:

<!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); 
    } 

Service1.cs:

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

我试过了好几天搞清楚是什么2008解决方案之间的差异2010年以及为什么只有2010年的项目有效。但无济于事。有什么建议么 ?

谢谢。

+0

你有几个不完整的单词请编辑你的问题。 **第一句话使NO SENSE。** –

+0

@Ramhound谢谢!我编辑过它。 – liorafar

回答

1

我在挖掘了它的网络之后,才得到它的工作。
的原因是因为我以为 - 在Web.config文件
是,在Visual Studio 2010中的项目与.Net4.0创建而Visual Studio 2008中创建了.NET3.5
该项目产生的差异.Net4.0中的Web.config文件与在.Net3.5中生成的文件不同.Net3.5
我需要的是使用“basicHttpBinding”为服务添加一个“端点”以便WCF服务工作与SOAP。

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

我发现这个信息感谢this link
我的新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> 

感谢的人谁试图帮助!

相关问题