我有以下代码在 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 项目有效。但无济于事。有什么建议么 ?
谢谢。