通过控制台应用程序的WCF REST

时间:2012-08-21 17:21:23

标签: asp.net wcf rest console-application

我正在尝试联系控制台应用中托管的基本REST服务(出于特定于我们产品的原因,我们无法在IIS中托管)。我可以访问localhost:8002 / Session_REST /,并且wsdl显示正常。但是,如果我尝试localhost:8002 / Session_REST / HelloWorld_GET或localhost:8002 / Session_REST / HelloWorld_GET / - 我得到的全部是405错误:找不到方法。

应用配置:

<?xml version="1.0"?>
<configuration>
    <system.serviceModel>
        <services>
            <service name="Proprietary.WebServices.Session_REST" behaviorConfiguration="servBehavior">
                <host>
                    <baseAddresses>
                        <add baseAddress="http://localhost:8002/Session_REST/"/>
                    </baseAddresses>
                </host>
                <endpoint address="xmlService" binding="webHttpBinding" behaviorConfiguration="restBehavior" contract="Proprietary.WebServices.ISession_REST">
                    <identity>
                        <userPrincipalName value="warren_thompson@proprietary.com" />
                    </identity>                    
                </endpoint>
                <endpoint address="mex" contract="IMetadataExchange" binding="mexHttpBinding" />
            </service>
        </services>
        <behaviors>
            <endpointBehaviors>
                <behavior name="restBehavior">
                    <webHttp helpEnabled="true"/>
                </behavior>
            </endpointBehaviors>
        </behaviors>
    </system.serviceModel>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
    </startup>
</configuration>

控制台应用:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceProcess;
using System.ServiceModel;
using Proprietary.WebServices;
using Proprietary.Framework;

namespace MyTestConsole
{
    class Program
    {
        static void Main(string[] args)
        {

            ServiceHost testServiceHost2 = null;

            try
            {
                testServiceHost2 = new ServiceHost(typeof(Session_REST));

                testServiceHost2.Open();
                Console.WriteLine("Server Started");
                Console.ReadLine();
            }
            finally
            {
                if (testServiceHost2 != null)
                {
                    ((IDisposable)testServiceHost2).Dispose();
                }
            }
        }
    }
}

WCF合同:使用系统;

using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using Proprietary.Framework;
using System.IO;

namespace Proprietary.WebServices
{

    [ServiceContract]
    public interface ISession_REST
    {

        [OperationContract]
        [WebInvoke(BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)]
        string HelloWorld_POST();


        [OperationContract]
        [WebInvoke(Method="GET")]
        string HelloWorld_GET();
    }


}

WCF实施:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Text;

using Proprietary.Framework;
using Proprietary.WebSupport;
using System.Web.Script.Serialization;

namespace Proprietary.WebServices
{
    /// <summary>
    /// Represents a doclink web service session - Rest version.
    /// </summary>
    public class Session_REST : ISession_REST
    {
        string ISession_REST.HelloWorld_POST()
        {
            return "Hellow World via POST";
        }

        string ISession_REST.HelloWorld_GET()
        {
            return "Hello World via GET";
        }
    }
}

1 个答案:

答案 0 :(得分:0)

好的 - 所以事实证明我没注意端点地址 - 这是xmlService - 所以我可以从localhost:8002 / Session_REST / xmlService / HelloWorld_GET调用它。

服务方法= baseaddress / endpointaddress / methodname /