一、接口
1,IwcfService
public interface IwcfService {
[OperationContract]
[WebGet(UriTemplate = "Get")]
string Get();
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "POST")
]
string Post(Stream pstream);
[OperationContract]
[WebInvoke(Method = "PUT", UriTemplate = "PUT")]
string Put(Stream pstream);
[OperationContract]
[WebInvoke(BodyStyle = WebMessageBodyStyle.WrappedRequest,
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
Method = "POST")]
string Post1(string value, string content);
}
2,wcfService
public class wcfService : IwcfService {
public string Get() { return "1"; }
public string Post(Stream pstream) {
using (var sr = new StreamReader(pstream)) {
var data = sr.ReadToEnd();
Logs("WCF", LogType.RECV, 0, data);
return data;
}
}
public string Post1(string value, string content) {
var m = new {
value = value,
content = content
};
var data = m.Json();
Logs("WCF", LogType.RECV, 0, data);
return data;
}
public string Put(Stream pstream) {
using (var sr = new StreamReader(pstream)) {
var data = sr.ReadToEnd();
Logs("WCF", LogType.RECV, 0, data);
return data;
}
}
}
二、配置文件
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service behaviorConfiguration="ServiceConfig" name="Con_Servicewcf.wcfService">
<endpoint address="http://localhost:9001/wcfService/" behaviorConfiguration="webHttp"
binding="webHttpBinding" bindingConfiguration="MyServiceBinding"
contract="Con_Servicewcf.IwcfService" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:9001/wcfService/" />
</baseAddresses>
</host>
</service>
</services>
<bindings>
<basicHttpBinding>
<binding name="WebService1Soap" closeTimeout="00:10:00" receiveTimeout="00:20:00" sendTimeout="00:20:00" maxReceivedMessageSize="2147483647"/>
</basicHttpBinding>
<customBinding>
<binding name="WebService1Soap12">
<textMessageEncoding messageVersion="Soap12"/>
<httpTransport/>
</binding>
<binding name="customBinding">
<binaryMessageEncoding>
</binaryMessageEncoding>
<httpTransport maxReceivedMessageSize="2147483647">
</httpTransport>
</binding>
</customBinding>
<webHttpBinding>
<binding name="MyServiceBinding" closeTimeout="00:10:00" receiveTimeout="00:20:00" sendTimeout="00:20:00" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" crossDomainScriptAccessEnabled="true">
<security mode="None">
<transport clientCredentialType="None"></transport>
</security>
</binding>
</webHttpBinding>
</bindings>
<behaviors>
<endpointBehaviors>
<behavior name="webHttp"><!--必须设置-->
<webHttp helpEnabled="true" automaticFormatSelectionEnabled="true"/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="ServiceConfig">
<serviceMetadata httpGetEnabled="true" httpGetUrl="http://localhost:9001/wcfService/"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
<dataContractSerializer maxItemsInObjectGraph="2147483647"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
</system.serviceModel>
</configuration>
三、启动服务
using (var host = new ServiceHost(typeof(wcfService))) {
Logs("WCF", LogType.OPEN, 0, "WCF Servise Starting");
host.Open();
var url = host.BaseAddresses[0].ToString();
Logs("WCF", LogType.OPEN, 0, "WCF Servise URL." + url);
Logs("WCF", LogType.OPEN, 0, "WCF Servise Open Success");
Console.Read();
}
四、参考资料
1.https://www.cnblogs.com/adingfirstlove/p/9835724.html