IIS托管WCF服务消耗时出现405错误

时间:2012-11-09 10:26:43

标签: asp.net wcf web-services

我正在IIS 7上托管我的WCF服务,即使它在使用WCF测试客户端时工作正常,我也无法从简单的控制台应用程序中使用我的服务。

我已经允许在IIS上处理.svc和.wsdl文件。我不确定我还缺少什么。

这是我的代码:

namespace EvalServiceLibrary {
    [ServiceContract]
    public interface IEvalService {
        [OperationContract]
        void SubmitEval(Eval eval);

        [OperationContract]
        List<Eval> GetEvals();

        [OperationContract]
        void RemoveEval(String id);
    }
}

namespace EvalServiceLibrary {
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
    public class EvalService : IEvalService {
        #region
        List<Eval> evals = new List<Eval>();

        public void SubmitEval(Eval eval) {
            eval.Id = Guid.NewGuid().ToString();
            evals.Add(eval);
        }

        public List<Eval> GetEvals() {
            return evals;
        }

        public void RemoveEval(String id) {
            evals.Remove(evals.Find(e => e.Id.Equals(id)));
        }
        #endregion
    }
}

我的web.config

<?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0"/>
  </system.web>
  <system.serviceModel>
    <services>
      <service name="EvalServiceLibrary.EvalService">
        <endpoint address="http://localhost/WebEvalService" binding="basicHttpBinding"
          bindingConfiguration="" name="basicHttpBinding" contract="EvalServiceLibrary.IEvalService" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- 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="false"/>
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
</configuration>

这是我的客户代码:

static void Main(string[] args) {
            EvalServiceClient client = new EvalServiceClient();

            client.SubmitEval(new Eval() { Comments = "Lorem Ipsum", Submitter = "egarcia", TimeSubmitted = DateTime.Now });

            List<Eval> evalList = client.GetEvals().ToList();

            foreach(var eval in evalList) {
                Console.WriteLine(eval.Id);
            }

            client.Close();
        }

我也可以访问本地IIS上的服务。

1 个答案:

答案 0 :(得分:0)

web.config文件中的服务端点声明存在问题。试试这个,

  <service name="EvalServiceLibrary.EvalService">

    <!-- This endpoint is exposed at the http://localhost/EvalServiceLibrary/EvalService.svc-->
    <endpoint address="" binding="basicHttpBinding" bindingConfiguration="" name="basicHttpBinding" contract="EvalServiceLibrary.IEvalService" />

    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
  </service>

基本上,

  1. 您的端点地址无效。事实上,您无需指定。

  2. 添加MexEndpoint。