jQuery调用WCF服务疑惑

飞起来一脚 2011-08-08 10:11:20
页面
<!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></title>
<script src="jquery-1.3.1.min.js" type="text/javascript"></script>
</head>
<body>
<script>
function Speak() {
var pople = {a:1,b:2};
$.ajax({
type: 'post',
url: '/JS_WCF/MyServices.svc/Add',
contentType: 'text/json',
data: JSON2.stringify(pople),
success: function(msg) {
alert(msg);
}
});
}
</script>
</body>
</html>


服务
App_Code/MyServices.cs
using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;


/// <summary>
///MyServices 的摘要说明
/// </summary>
///
[ServiceContract(Namespace="")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class MyServices
{
[OperationContract]
[WebInvoke(RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "/Add", BodyStyle = WebMessageBodyStyle.WrappedRequest)]
public int Add(int a,int b)
{
//
//TODO: 在此处添加构造函数逻辑
//
return a + b;
}
}


MyServices.svc
<%@ ServiceHost Language="C#" Debug="true" Service="MyServices" CodeBehind="~/App_Code/MyServices.cs" %>


web.config
  <system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="me">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"></serviceHostingEnvironment>
<services>
<service name="MyServices">
<endpoint address="" binding="webHttpBinding" contract="MyServices" behaviorConfiguration="me">
</endpoint>
</service>
</services>
</system.serviceModel>


全部代码如上,运行无误

疑惑:
1 MyServices.svc文件必须放到当前的网站下吗,如果放到其他的类库中是否可以
2 vs08网站下建立a.svc和a.svc.cs并关联起来(就像a.aspx和a.aspx.cs),该怎么操作
...全文
220 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
飞起来一脚 2011-08-08
  • 打赏
  • 举报
回复
新建了一个wcf服务应用程序(WcfService1)到同一个解决方案中,使用默认
ISerivce1.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Activation;

namespace WcfService1
{
// 注意: 如果更改此处的接口名称 "IService1",也必须更新 Web.config 中对 "IService1" 的引用。
[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode=AspNetCompatibilityRequirementsMode.Allowed)]
public interface IService1
{

[OperationContract]
string GetData(int value);

[OperationContract]
CompositeType GetDataUsingDataContract(CompositeType composite);

// 任务: 在此处添加服务操作
}


// 使用下面示例中说明的数据约定将复合类型添加到服务操作。
[DataContract]
public class CompositeType
{
bool boolValue = true;
string stringValue = "Hello ";

[DataMember]
public bool BoolValue
{
get { return boolValue; }
set { boolValue = value; }
}

[DataMember]
public string StringValue
{
get { return stringValue; }
set { stringValue = value; }
}
}
}

Service1.svc.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Activation;

namespace WcfService1
{
// 注意: 如果更改此处的类名“Service1”,也必须更新 Web.config 和关联的 .svc 文件中对“Service1”的引用。
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1 : IService1
{
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}

public CompositeType GetDataUsingDataContract(CompositeType composite)
{
if (composite.BoolValue)
{
composite.StringValue += "Suffix";
}
return composite;
}
}
}


web中
WcfService.svc
<%@ ServiceHost Debug="true" Language="C#" Service="WcfService1.Service1"%>


页面

<!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 runat="server">
<title></title>
<script src="jquery-1.2.6.js" type="text/javascript"></script>
<script src="JSON2.js" type="text/javascript"></script>
</head>
<body>
<form id="form1" runat="server">
<div>

<script>
function Speak() {
$.ajax({
type: 'post',
url: '/JS_WCF/WcfService1.svc/GetData',
contentType: 'text/json',
data: 1,
success: function(msg) {
alert(msg);
}
});
}
Speak();
</script>

</div>
</form>
</body>
</html>


web.config
  <system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="bbb">
<webHttp/>
</behavior>
<behavior name="other">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"></serviceHostingEnvironment>
<services>
<service name="MyServices">
<endpoint address="" binding="webHttpBinding" contract="MyServices" behaviorConfiguration="bbb">
</endpoint>
</service>
<service name="WcfService1">
<endpoint binding="webHttpBinding" contract="WcfService1.IService1" behaviorConfiguration="other"></endpoint>
</service>
</services>
</system.serviceModel>
</configuration>


相当于svc的代码指定到wcf服务应用程序的服务类,web.config配置了一下终结点,可是没有任何反应
访问http://localhost:3804/JS_WCF/WcfService1.svc,提示
服务“WcfService1.Service1”有零个应用程序(非基础结构)终结点。这可能是因为未找到应用程序的配置文件,或者在配置文件中未找到与服务名称匹配的服务元素,或者服务元素中未定义终结点。

不知怎么回事?
truecoffeefox 2011-08-08
  • 打赏
  • 举报
回复
svc要放到web里,至于svc的code可以随意放在那个类库里

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧