0

我是 WCF 的新手。我在 VS2010 (WCF service appl) 中创建了一个 WCF restful 服务。它针对的是 Framework 4.0。我在本地 IIS 上托管了这项服务,并为框架 4.0 设置了应用程序池。当我从浏览器或提琴手调用 appl 方法时,它们工作正常。现在,我创建了一个基于客户端控制台。当我从客户端调用任何方法时,我得到以下通信异常:

** 远程服务器返回意外响应:(405) Method Not Allowed。**

服务接口文件:

namespace MyService

{

[ServiceContract]
public interface ITestService
{


    [WebGet(UriTemplate = "GetDateTime")]
    [OperationContract]
    string GetDateTime();

    [WebGet(UriTemplate = "GetName")]
    [OperationContract]
    string GetName();

}

}

实现上述接口的类:

namespace MyService

{

 public class TestService : ITestService
    {
        public string GetDateTime()
        {
            return DateTime.Now.ToString();
        }

    public string GetName()
    {
        return "MY name is KingKong";
    }
}

}

Web.config 文件:

  <system.serviceModel>
<services>
  <service behaviorConfiguration="ServiceBehavior" name="MyService.TestService">
    <endpoint address="" behaviorConfiguration="web" binding="webHttpBinding" bindingConfiguration="" contract="MyService.ITestService">

    </endpoint>
  </service>
</services>
<behaviors>
  <endpointBehaviors>
    <behavior name="web">
      <webHttp />
    </behavior>
  </endpointBehaviors>
  <serviceBehaviors>
    <behavior name="ServiceBehavior">
      <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
      <serviceMetadata httpGetEnabled="true" />
      <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
      <serviceDebug includeExceptionDetailInFaults="false" />
    </behavior>
  </serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />

测试服务.svc

<%@ ServiceHost Language="C#" Debug="true" Service="MyService.TestService" CodeBehind="TestService.svc.cs" %>

客户端 app.config:

<system.serviceModel>
    <behaviors>
        <endpointBehaviors>
            <behavior name="">
                <webHttp />
            </behavior>
        </endpointBehaviors>
    </behaviors>
    <client>
        <endpoint address="http://localhost/BestService/TestService.svc"
            binding="webHttpBinding" bindingConfiguration="" contract="MyService.ITestService"
            name="MyClientConfig" kind="" endpointConfiguration="" />
    </client>
</system.serviceModel>

客户端程序调用代理类

 static void Main(string[] args)
    {
        TestServiceClient proxy = null;
        try
        {
            proxy = new TestServiceClient();

            Console.WriteLine("Test 1: List all products");
            string sdatetime = proxy.GetName();

            Console.WriteLine("Datetime: {0}", sdatetime);

            Console.WriteLine();

            // Disconnect from the service
            proxy.Close();


        }
        catch (CommunicationException cex)
        {

            if (cex.InnerException != null)
            {
                Console.WriteLine("{0}", cex.InnerException.Message);
            }
            else
            {
                Console.WriteLine("General exception: {0}", cex.Message);
            }
        }
        catch (Exception e)
        {
            if (e.InnerException != null)
            {
                Console.WriteLine("{0}", e.InnerException.Message);
            }
            else
            {
                Console.WriteLine("General exception: {0}", e.Message);
            }
        }

        Console.WriteLine("Press ENTER to finish");
        Console.ReadLine();
    }

我添加了对应用程序的服务引用,Reference.cs 文件包含以下部分代码:

命名空间 MyClient.MyService {

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
[System.ServiceModel.ServiceContractAttribute(ConfigurationName="MyService.ITestService")]
public interface ITestService {

    [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/ITestService/GetDateTime", ReplyAction="http://tempuri.org/ITestService/GetDateTimeResponse")]
    string GetDateTime();

    [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/ITestService/GetName", ReplyAction="http://tempuri.org/ITestService/GetNameResponse")]
    string GetName();
}

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
public interface ITestServiceChannel : MyClient.MyService.ITestService, System.ServiceModel.IClientChannel {
}

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
public partial class TestServiceClient : System.ServiceModel.ClientBase<MyClient.MyService.ITestService>, MyClient.MyService.ITestService {

    public TestServiceClient() {
    }

    public TestServiceClient(string endpointConfigurationName) : 
            base(endpointConfigurationName) {
    }

    public TestServiceClient(string endpointConfigurationName, string remoteAddress) : 
            base(endpointConfigurationName, remoteAddress) {
    }

    public TestServiceClient(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) : 
            base(endpointConfigurationName, remoteAddress) {
    }

    public TestServiceClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) : 
            base(binding, remoteAddress) {
    }

    public string GetDateTime() {
        return base.Channel.GetDateTime();
    }

    public string GetName() {
        return base.Channel.GetName();
    }
}

}

请帮助我,因为我花了将近 2 天的时间试图找出客户的问题

谢谢

4

1 回答 1

0

VS2010 没有为 RESTFUL 服务生成代理的能力,因为它为基于 SOAP 的服务生成代理。所以解决方案是编写自己的代理类,它继承自 ClientBase 类,并使用 Channel 调用服务的 Web 方法。

于 2011-02-09T23:07:55.490 回答