WCF错误 - 不允许的方法

时间:2013-05-03 21:13:44

标签: c# .net wcf iis

运行WCF测试客户端并在本地调试WCF服务时,一切正常。在将其部署到远程服务器上的IIS然后尝试使用WCF测试客户端加载之后,我开始将“对象未设置为对象实例”错误。我在服务中添加了一个try-catch,错误更改为“Method not allowed”。

我一直在阅读这些错误,大多数帖子都与使用JSON或AJAX访问其WCF客户端的人有关。我尝试了许多建议的解决方案,其中大部分都是对web.config的更改。我只是在这个阶段使用测试客户端访问它,一旦证明有效,它将由Winform应用程序使用。

虽然存在信任设置,但在服务上公开的2个方法都会创建与不同域上的SQL服务器的连接。我认为这可能是问题的一部分。

我的服务界面

[ServiceContract]
public interface IMeterQueryService
{
    [OperationContract]
    List<Meter> FindMeter(string mprn);

已实施服务

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
public class MeterQueryService : IMeterQueryService
{
    public List<Meter> FindMeter(string mprn)
    {
        using (var da = new DataAccess())
        {

        var meters = da.GetMeter(mprn);
        da.Dispose();
        return meters;

        }
    }

App配置文件

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" />
  </system.web>
  <connectionStrings>
    <add name="Tims" connectionString="hidden" providerName="System.Data.SqlClient"/>
  </connectionStrings>
  <!-- When deploying the service library project, the content of the config file must be added to the host's 
  app.config file. System.Configuration does not support config files for libraries. -->
  <system.serviceModel>
    <services>
      <service name="TimsAPI.MeterQueryService">
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, 
          set the values below to false 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="True" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

web配置文件,站点引用WCF服务库。

<?xml version="1.0"?>
<configuration>

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="false" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5"/>
  </system.web>
  <system.serviceModel>
    <services>
      <service name="TimsAPI.MeterQueryService">
        <endpoint address="http://easvr33:1000/" binding="basicHttpContextBinding" bindingConfiguration=""
          contract="TimsAPI.IMeterQueryService" />
        <endpoint address="mex" binding="mexHttpBinding" bindingConfiguration=""
          contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="localhost" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="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="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
        <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="false" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true"/>
  </system.webServer>

</configuration>

更新

我将WCF服务重新创建为WCF服务网站,而不是库,现在一切正常。我认为2个配置文件之间有些奇怪。我仍然有兴趣知道为什么它从未奏效但我也明白,对于没有完全访问源的人来说可能很难诊断

2 个答案:

答案 0 :(得分:0)

开始时可能是服务SVC的问题。来自here

  

1.打开虚拟目录的属性。

     

2.转到“目录”选项卡。

     

3.单击“配置”。

     

4.点击添加...

     

5.提供以下信息:

     

a.Executable:C:\ WINDOWS \ Microsoft.NET \ Framework \ v2.0.50727 \ aspnet_isapi.dll

     

b.Extension:.svc

     

v.Verbs:GET,HEAD,POST,DEBUG

答案 1 :(得分:0)

当通过IIS网站托管WCF服务时,不使用服务app.config,而是加载了管道网站的web.config。必须将服务工作所需的所有设置复制到托管站点的web.config