我从几天开始尝试使用 wcf 宁静的服务,但它给了我不好的要求。请帮帮我。
这是我的配置文件
<system.serviceModel>
<bindings>
<webHttpBinding>
<binding name="state" allowCookies="true">
<security mode="None"></security>
</binding>
</webHttpBinding>
</bindings>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehaviour">
<serviceMetadata httpGetEnabled="True"/>
<serviceDebug includeExceptionDetailInFaults="True"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="ServiceBehaviour" name="RESTFUL_DEMO.Web.services.Calc">
<endpoint address="" bindingConfiguration="state" binding="webHttpBinding" name="Http" contract="RESTFUL_DEMO.Web.services.ICalc"/>
<endpoint address="abcd" binding="wsHttpBinding" name="wsHttp" contract="RESTFUL_DEMO.Web.services.ICalc"/>
<endpoint address="mex" binding="mexHttpBinding" name="MEX" contract="IMetadataExchange"/>
</service>
</services>
我的服务合同和数据合同界面如下。
[ServiceContract(SessionMode = SessionMode.Allowed)]
[XmlSerializerFormat]
public interface ICalc
{
[OperationContract]
[WebInvoke(UriTemplate = "dowork", Method = "POST", RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare)]
int DoWork(Enroll a);
}
[DataContract]
public class Enroll
{
public Enroll()
{
}
public Enroll(string Avalue)
{
this.Avalue = Avalue;
}
[DataMember(IsRequired = true)]
public string Avalue
{
get;
set;
}
}
我使用该服务的代码如下
HttpWebRequest request = WebRequest.Create("http://localhost/RESTFUL_DEMO.Web/services/Calc.svc/dowork") as HttpWebRequest;
XmlDocument doc = new XmlDocument();
doc.Load(@"d:\test.xml");
string sXML = doc.InnerXml;
request.ContentLength = sXML.Length;
request.ContentType = "test/xml; charset=utf-8";
var sw = new StreamWriter(request.GetRequestStream());
sw.Write(sXML);
sw.Close();
WebResponse response = request.GetResponse();
StreamReader stream = new StreamReader(response.GetResponseStream());
String result = stream.ReadToEnd();