【问题标题】:WCF Error - Method Not AllowedWCF 错误 - 方法不允许
【发布时间】:2013-05-03 21:13:44
【问题描述】:

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

我整天都在阅读这些错误,大多数帖子都与使用 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;

        }
    }

应用配置文件

<?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 服务网站,而不是库,现在一切正常。我认为这两个配置文件之间有些奇怪。我仍然很想知道为什么它从来没有工作过,但我也明白对于没有完全访问源的人可能很难诊断

【问题讨论】:

  • 启用 WCF 跟踪以查看问题所在。
  • 我在您的托管网站 web.config 中看不到连接字符串。如果服务需要使用连接字符串访问数据库,则连接字符串需要位于托管应用程序的 web.config 中。您在服务中使用的其他配置参数也是如此。
  • 啊,那可能会有所作为。连接字符串在 WCF 站点使用的服务库的 app.config 中,我认为该站点也不需要 cs。会检查的。
  • 服务使用的配置文件是宿主应用的配置文件,而不是服务的app.config
  • 我将连接字符串移动到 web.config 并且它可以正常工作。浪费了一天的时间,我不觉得有点愚蠢!将其移入解决方案,我将标记为 answer.cheers 为您的帮助

标签: c# .net wcf iis


【解决方案1】:

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

【讨论】:

  • 申请赏金有最少的等待时间,明天我会坚持下去。
【解决方案2】:

一开始就为 SVC 提供服务可能存在问题。来自here

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

2.转到目录选项卡。

3.点击配置。

4.点击添加…

5.提供以下信息:

a.可执行文件:C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll

b.扩展名:.svc

c.动词:GET、HEAD、POST、DEBUG

【讨论】:

  • 我已将它作为网站部署在 IIS 上。它应该在应用程序中还是在网站内的 VD 中?
猜你喜欢
  • 2012-10-25
  • 2012-07-10
  • 2011-01-27
  • 1970-01-01
  • 2011-09-04
  • 1970-01-01
  • 1970-01-01
  • 2011-12-07
  • 2018-07-21
相关资源
最近更新 更多