【发布时间】:2014-10-04 03:20:19
【问题描述】:
我的服务合约,命名为IService1,如下
using System.ServiceModel;
using System.ServiceModel.Web;
namespace WcfServiceDemo
{
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json)]
string GetData(int Value);
}
}
这是我的服务实现,
using System.ServiceModel.Activation;
namespace WcfServiceDemo
{
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1 : IService1
{
public string GetData(int Value)
{
return string.Format("You entered: {0}", Value);
}
}
}
我的 web.config,
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name ="ServiceBehavior">
<!-- To avoid disclosing metadata information, set the value below to false 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="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="EndPointBehavior">
<enableWebScript />
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
<services>
<service name ="WcfDemoService" behaviorConfiguration="ServiceBehavior">
<endpoint address="" binding="webHttpBinding" contract="IService1" behaviorConfiguration="EndPointBehavior" />
</service>
</services>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<!--
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"/>
</system.webServer>
</configuration>
使用 WCF 服务的脚本,
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Client.aspx.cs" Inherits="WcfServiceDemo.Client" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
Enter a number:<input type="text" id="txtNum"/>
<input type="button" onclick="AlertEnteredNum();"/>
</div>
</form>
<script type="text/javascript" src="Script/jquery-1.3.2.min.js"></script>
<script type="text/javascript">
function AlertEnteredNum() {
var Value = $('#txtNum').val();
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: 'Service1.svc/GetData',
data: '{"Value": "' + Value + '"}',
dataType: "json",
processData: false,
success: function (data) {
alert("Success: " + data.d);
},
error: function (result) {
alert("error: " + result);
}
});
}
</script>
</body>
</html>
总是错误回调被执行。有人可以告诉我我在这里缺少什么吗?
我想我需要在 IIS 上托管,因为我在我的配置中设置了兼容模式。但, 当我在 IIS 上托管此服务时,我收到解析器错误为“无法识别的属性 'targetFramework'。请注意,属性名称区分大小写。”
而且,如果我删除这个“targetFramework = 4”属性,我会收到一个异常“无法加载文件或程序集‘WcfServiceDemo’或其依赖项之一。此程序集由比当前加载的运行时更新的运行时构建运行时,无法加载。”
【问题讨论】:
-
尝试数据:{Value:$('#txtNum').val()}
-
试过了,还是不行。还有其他问题……