3

我是 .net 的新手,对 WCF 知之甚少,所以如果有任何愚蠢的问题,请多多包涵。如果我的代码没有显式生成任何线程,我想知道 WCF 如何处理 SELF-HOST 场景中的同时调用。因此,在阅读了很多关于 stackoverflow 的内容后,我创建了一个测试应用程序,但它似乎无法正常工作。请指教。非常感谢。

请注意 ...

  1. 我的问题只是关于 WCF SELF HOSTING,所以请不要参考任何与 IIS 相关的内容。
  2. 我正在使用webHttpBinding
  3. 我知道有 maxConnection 和服务限制设置,但我只对我的研究设置中的2 个同时调用感兴趣。所以不应该有最大 conn 或线程池问题。
  4. 我的测试服务没有使用会话。

代码如下...

namespace myApp
{
  [ServiceContract(SessionMode = SessionMode.NotAllowed)]
  public interface ITestService
  {
    [OperationContract]
    [WebGet(UriTemplate="test?id={id}")]
    string Test(int id);
  }

  [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, 
                   ConcurrencyMode = ConcurrencyMode.Multiple)]
  public class TestService : ITestService
  {
    private static ManualResetEvent done = new ManualResetEvent(false);

    public string Test(int id)
    {
      if (id == 1)
      {
        done.Reset();
        done.WaitOne();
      }
      else
      {
        done.Set();
      }
    }
  } 
}

应用程序配置...

  <system.serviceModel>
    <behaviors>
      <endpointBehaviors>
        <behavior name = "TestEndpointBehavior">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <services>
      <service name = "myApp.TestService">
        <endpoint address = "" behaviorConfiguration="TestEndpointBehavior"
                  binding = "webHttpBinding"
                  contract = "myApp.ITestService">
        </endpoint>
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8080/test/"/>
          </baseAddresses>
        </host>
      </service>
    </services>
  </system.serviceModel>
  <system.web>
    <sessionState mode = "Off" />
  </system.web>

我是如何测试...

应用程序运行后,我打开浏览器,FF 以防万一,调用http://localhost:8080/test/test?id=1。该请求使应用程序暂停等待信号,即WaitOne。然后在另一个浏览器选项卡中再次调用http://localhost:8080/test/test?id=2。预期的是此请求将设置信号,因此服务器将为这两个请求返回。

但我看到应用程序挂起,第二个请求从未输入测试功能。所以显然我的代码不支持同时/并发调用。哪里不对了?

4

1 回答 1

0

您可以使用单个类来设置您的 wcf 服务并丢弃接口。您还需要添加 global.asax 文件。在您拨打第二个电话后,他们都将返回“完成”。

此配置可以满足您的要求。使用以下命令创建 TestService.cs:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall,
             ConcurrencyMode = ConcurrencyMode.Multiple)]
[ServiceContract(SessionMode = SessionMode.NotAllowed)]
public class TestService
{
    private static ManualResetEvent done = new ManualResetEvent(false);

    [OperationContract]
    [WebGet(UriTemplate = "test?id={id}")]
    public string Test(int id)
    {
        if (id == 1)
        {
            done.Reset();
            done.WaitOne();
        }
        else
        {
            done.Set();
        }
        return "finished";
    }


}

网络配置:

<configuration>
<system.web>
  <compilation debug="true" targetFramework="4.0" />
</system.web>
<system.webServer>
 <modules runAllManagedModulesForAllRequests="true" />
</system.webServer>
<system.serviceModel>
<standardEndpoints>
<webHttpEndpoint>
    <!-- 
        Configure the WCF REST service base address via the global.asax.cs file and the default endpoint 
        via the attributes on the <standardEndpoint> element below
    -->
<standardEndpoint name="" helpEnabled="false"  >    </standardEndpoint>


  </webHttpEndpoint>
</standardEndpoints>
<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 aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
</configuration>

Global.asax 文件:

public class Global : System.Web.HttpApplication
{

    protected void Application_Start(object sender, EventArgs e)
    {
        RouteTable.Routes.Add(new ServiceRoute("testservice", new WebServiceHostFactory(), typeof(TestService)));
    }
}
于 2012-04-03T15:17:32.470 回答