Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
是否可以先执行契约WCF并公开REST/JSON和SOAP/XML端点?_Xml_Json_Wcf_Rest_Contract First - Fatal编程技术网

是否可以先执行契约WCF并公开REST/JSON和SOAP/XML端点?

是否可以先执行契约WCF并公开REST/JSON和SOAP/XML端点?,xml,json,wcf,rest,contract-first,Xml,Json,Wcf,Rest,Contract First,目前,我们使用.NET3.5和WSCF.blue编写合同第一个WCFSOAP服务。 这允许我们设计使用Xsd文件交换的Xml文档 既然WSDL2.0已经存在,您可以为REST端点设计契约,并且.NET 4.5中对契约优先提供了适当的支持,那么我们有以下问题: 是否可以升级到Visual Studio 2012,保留现有的Xsd集并自动公开REST和/或SOAP端点 是否可以升级到Visual Studio 2012,保留现有的Xsd集并自动交换Xml和/或Json文档?以下是我的解决方案: 您至

目前,我们使用.NET3.5和WSCF.blue编写合同第一个WCFSOAP服务。 这允许我们设计使用Xsd文件交换的Xml文档

既然WSDL2.0已经存在,您可以为REST端点设计契约,并且.NET 4.5中对契约优先提供了适当的支持,那么我们有以下问题:

是否可以升级到Visual Studio 2012,保留现有的Xsd集并自动公开REST和/或SOAP端点

是否可以升级到Visual Studio 2012,保留现有的Xsd集并自动交换Xml和/或Json文档?

以下是我的解决方案:

您至少需要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; }
    }
}
要么手工编写数据契约,要么用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=自动rest帮助页面

/RestSoapTest.svc/url/to/method=执行REST操作

对于Get方法,请使用WebGet并确保UriTemplate中的参数计数与方法原型匹配,否则将出现错误

对于Put方法,使用WebInvoke并确保UriTemplate中的参数计数等于prototype中的计数+1

如果这样做,WCF框架将假定任何未映射的参数都是通过HTTP Post的请求体传入的

如果需要通过主体输入多个参数,则需要将参数格式设置为Wrapped,默认为Bare。自动包装parameterName/value属性中的多个参数