WCF的HTTP服务
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "/CheckCOM",
BodyStyle = WebMessageBodyStyle.WrappedRequest,
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
string CheckCOM(int id);
public string CheckCOM(int id = 0)
{
List<string> ComList = new List<string>();
//检查是否含有串口
string[] str = SerialPort.GetPortNames();
if (str == null)
{
model.Success = false;
model.Msg = "获取串口信息失败,没有检测到串口存在";
}
else
{
Logger.Info(DateTime.Now.ToShortTimeString() + "已成功调用了服务一次。");
//添加串口项目
foreach (string comitem in System.IO.Ports.SerialPort.GetPortNames())
{
//获取有多少个COM口
ComList.Add(comitem);
}
model.Success = true;
model.Msg = "获取串口信息成功";
model.Data = SerializeObject(ComList);
}
Logger.Info(DateTime.Now.ToShortTimeString() + "串口服务返回值:"+ model.ToJson());
return model.ToJson();
}
app.config里面配置:
<!-- 部署服务库项目时,必须将配置文件的内容添加到
主机的 app.config 文件中。System.Configuration 不支持库的配置文件。 -->
<system.serviceModel>
<services>
<service name="WcfServiceLib.ComLightService" behaviorConfiguration="serviceBehavior">
<endpoint address="" binding="webHttpBinding" contract="WcfServiceLib.IComLightService" behaviorConfiguration="web">
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://127.0.0.1:8081/ComLightService" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="serviceBehavior">
<!-- 为避免泄漏元数据信息,
请在部署前将以下值设置为 false -->
<serviceMetadata httpGetEnabled="True"/>
<!-- 要接收故障异常详细信息以进行调试,
请将以下值设置为 true。在部署前设置为 false
以避免泄漏异常信息 -->
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>

浙公网安备 33010602011771号