您的位置:首页 > 其它

使用VS2008创建发布一个简单的WCF服务,并引入到web应用程序

2013-03-05 12:14 701 查看
先创建一个 空白解决方方案 命名为:WCFApp

创建WCF服务

1:Visual c# -- Web -- WCF 服务应用程序





删除 service1.svc 及 Iservice1.cs

2::添加新建项 -- Visual c# -- WCF 服务 -- 改 Service1.svc 为 Calculator.svc --确定

系统自动添加 Calculator.svc,Calculator.svc.cs ,ICalculator.cs 3个文件

修改 ICalculator.cs 代码:
namespace WcfService1
{
// 注意: 如果更改此处的接口名称 "ICalculator",也必须更新 Web.config 中对 "ICalculator" 的引用。
[ServiceContract]
public interface ICalculator
{
[OperationContract]
double Add(double n1, double n2);
[OperationContract]
double Subtract(double n1, double n2);
[OperationContract]
double Multiply(double n1, double n2);
[OperationContract]
double Divide(double n1, double n2);
}
}

修改 Calculator.svc.cs 代码:

namespace WcfService1
{
// 注意: 如果更改此处的类名 "Calculator",也必须更新 Web.config 中对 "Calculator" 的引用。
//[ServiceBehavior(
//AutomaticSessionShutdown = true,
//ConcurrencyMode = ConcurrencyMode.Single,
//InstanceContextMode = InstanceContextMode.PerSession,
//IncludeExceptionDetailInFaults = false,
//UseSynchronizationContext = true,
//ValidateMustUnderstand = true)]

public class Calculator : ICalculator
{
public double Add(double n1, double n2)
{
return n1 + n2;
}
public double Subtract(double n1, double n2)
{
return n1 - n2;
}
public double Multiply(double n1, double n2)
{
return n1 * n2;
}
public double Divide(double n1, double n2)
{
return n1 / n2;
}
}
}


3::添加新建项 -- Visual c# --代码 -- 代码文件-- 把整个名称改为 service.svc -- 确定

这样创建了一个空的文档

输入代码:

<</span>%@ServiceHostlanguage="C#" Debug="true" Service="WcfService1.Calculator"%>

//注意去匹配 加粗 颜色一样的部分

4: 配置 Web.config

其他部分勿动 添加:

到此 wcf 创建完成

5:发布WCF到服务器空间 





输入网址:http://www.XXX.com/service.svc 具体根据你的服务器的地址

记住后面一定要加 service.svc



到此wcf服务发布成功

创建asp.net web 应用程序

1:Visual c#









2:Default.aspx

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ServiceReference1.CalculatorClient client = new ServiceReference1.CalculatorClient();
double value1 = 100.00D;
double value2 = 15.99D;
double result = client.Add(value1, value2);
//Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result);
Label1.Text += "
" + string.Format("Add({0},{1}) = {2}", value1, value2, result);

// Call the Subtract service operation.
value1 = 145.00D;
value2 = 76.54D;
result = client.Subtract(value1, value2);
Label1.Text += "
" + string.Format("Subtract({0},{1}) = {2}", value1, value2, result);

// Call the Multiply service operation.
value1 = 24.00D;
value2 = 81.25D;
result = client.Multiply(value1, value2);
Label1.Text += "
" + string.Format("Multiply({0},{1}) = {2}", value1, value2, result);

// Call the Divide service operation.
value1 = 22.00D;
value2 = 7.00D;
result = client.Divide(value1, value2);
Label1.Text += "
" + string.Format("Divide({0},{1}) = {2}", value1, value2, result);

//Closing the client releases all communication resources.
client.Close();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: