首先创建wcf service服务
新建一个服务,命名为IService4
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace WcfService1
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService4" in both code and config file together.
[ServiceContract(Name = "te", Namespace = "te")]
interface IService4
{
[OperationContract]
string DoWork();
[OperationContract]
string GetData(int value);
[OperationContract]
CompositeType GetDataUsingDataContract(CompositeType composite);
}
}
service4.svc代码并设置为启动项
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace WcfService1
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service4" in code, svc and config file together.
// NOTE: In order to launch WCF Test Client for testing this service, please select Service4.svc or Service4.svc.cs at the Solution Explorer and start debugging.
public class Service4 : IService4
{
public string DoWork()
{
return "test";
}
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
public CompositeType GetDataUsingDataContract(CompositeType composite)
{
if (composite == null)
{
throw new ArgumentNullException("composite");
}
if (composite.BoolValue)
{
composite.StringValue += "Suffix";
}
return composite;
}
}
}
右键发布即可,发布生成的config文件在system.serviceModule 里添加
<behaviors>
<serviceBehaviors>
<behavior name="CalculatorBehavior">
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="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>
</behaviors>
<services>
<service behaviorConfiguration="CalculatorBehavior" name="WcfService1.Service4">
<endpoint address="" binding="basicHttpBinding"
bindingConfiguration="" contract="WcfService1.IService4" />
</service>
<service behaviorConfiguration="CalculatorBehavior" name="WcfService1.Service2">
<endpoint address="" binding="basicHttpBinding"
bindingConfiguration="" contract="WcfService1.IService2" />
</service>
</services>
需要注意的是要修改svc里面的内容
比如Service2.svc内容是
<%@ ServiceHost Language="C#" Debug="true" Service="WcfService1.Service2" CodeBehind="Service2.svc.cs" %>
接着就可以发布服务了
依次填写就行
当时当我们访问http://127.0.0.1:8080/Service4.svc 假如报错如下
由于扩展配置问题而无法提供您请求的页面。如果该页面是脚本,请添加处理程序。如果应下载文件,请添加 MIME 映射
解决方案:CD到如下目录
C:\Windows\Microsoft.NET\Framework\v3.0\Windows Communication Foundation
运行ServiceModelReg.exe -i
假如报错:未能从程序集“System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”中加载类型“System.ServiceModel.Activation.HttpHandler”。
解决方案:把这些装上