目前,我们使用.NET 3.5和WSCF.blue编写合同第一个WCF SOAP服务。 这允许我们设计使用Xsd文件交换的Xml文档。
现在WSDL 2.0已存在并且您可以为REST端点设计合同,并且在.NET 4.5中首先对合同提供了适当的支持,我们有以下问题:
是否可以升级到Visual Studio 2012,保留现有的Xsd集并自动公开REST和/或SOAP端点?
是否可以升级到Visual Studio 2012,保留现有的Xsd集并自动交换Xml和/或Json文档?
答案 0 :(得分:0)
这是我的解决方案:
您需要最少的Visual Studio 2012。
创建WCF服务库项目。
包含您的Xsd文件以自动创建数据协定包装。
写下你的ServiceContract和类:
using System;
using System.Linq;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
namespace RestSoapTest
{
public class Service1 : IService1
{
public List<CompositeType> GetData(string paramA, string paramB)
{
List<CompositeType> output = new List<CompositeType>();
output.Add(new CompositeType()
{
Key = paramA,
Value = paramB,
});
output.Add(new CompositeType()
{
Key = paramA,
Value = paramB,
});
output.Add(new CompositeType()
{
Key = paramA,
Value = paramB,
});
return output;
}
public void PutData(string paramA, string paramB, List<CompositeType> data)
{
throw new NotImplementedException();
}
}
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebGet(UriTemplate = "GetData/{paramA}/{paramB}")]
List<CompositeType> GetData(string paramA, string paramB);
[OperationContract]
[WebInvoke(UriTemplate = "PutData/{paramA}/{paramB}")]
void PutData(string paramA, string paramB, List<CompositeType> data);
}
[DataContract]
public class CompositeType
{
[DataMember]
public string Key { get; set; }
[DataMember]
public string Value { get; set; }
}
}
(手动编写DataContract或用Xsd控制它)
添加Web主机项目,引用WCF项目并添加此web.config文件:
<?xml version="1.0"?>
<configuration>
<system.serviceModel>
<standardEndpoints>
<webHttpEndpoint>
<standardEndpoint name="standardRest" automaticFormatSelectionEnabled="true" defaultOutgoingResponseFormat="Json" helpEnabled="true"/>
</webHttpEndpoint>
<mexEndpoint>
<standardEndpoint name="standardMex"/>
</mexEndpoint>
</standardEndpoints>
<services>
<service name="RestSoapTest.Service1">
<endpoint name="rest" address="" kind="webHttpEndpoint" endpointConfiguration="standardRest" contract="RestSoapTest.IService1"/>
<endpoint name="mex" address="mex" kind="mexEndpoint" endpointConfiguration="standardMex" contract="RestSoapTest.IService1"/>
<endpoint name="soap" address="soap" binding="basicHttpBinding" contract="RestSoapTest.IService1"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment>
<serviceActivations>
<add relativeAddress="RestSoapTest.svc" service="RestSoapTest.Service1"/>
</serviceActivations>
</serviceHostingEnvironment>
</system.serviceModel>
<system.web>
<compilation debug="true"/>
</system.web>
</configuration>
现在您可以浏览到:
/RestSoapTest.svc = SOAP端点/帮助页面
/RestSoapTest.svc?wsdl = SOAP元数据
/RestSoapTest.svc/help =自动休息帮助页面
/RestSoapTest.svc/url/to/method =执行REST操作
对于Get方法,使用WebGet并确保UriTemplate和方法原型中的参数计数匹配,否则您将收到错误
对于Put方法,使用WebInvoke并确保UriTemplate中的参数计数等于(原型中的计数+ 1)。
如果这样做,WCF框架将假定任何单个未映射的参数通过HTTP Post的请求主体进入。
如果您需要通过正文输入多个参数,则需要将参数格式设置为Wrapped,默认为Bare。 Wrapped auto包装了parameterName / value属性中的多个参数。