代码之家  ›  专栏  ›  技术社区  ›  Prisoner ZERO

WCF服务模拟

  •  1
  • Prisoner ZERO  · 技术社区  · 16 年前

    大家好。。。

    显然,我没有为我的WCF服务正确设置模拟。我不想逐个方法设置安全性(在实际代码中)。这个服务(目前)是开放的,每个人都可以在内部网上调用它。

    问:为了使模拟工作正常,我需要在web配置中更改什么?

    服务Web.config看起来像。。。

    <configuration>
        <system.web>
            <authorization>
                <allow users="?"/>
            </authorization>
            <authentication mode="Windows"/>
            <identity impersonate="true" userName="MyDomain\MyUser" password="MyPassword"/>
        </system.web>
        <system.serviceModel>
            <services>
                <service behaviorConfiguration="wcfFISH.DataServiceBehavior" name="wcfFISH.DataService">
                        <endpoint address="" binding="wsHttpBinding" contract="wcfFISH.IFishData">
                        <identity>
                            <dns value="localhost"/>
                        </identity>
                    </endpoint>
                        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
                    </service>
                </services>
            <behaviors>
                <serviceBehaviors>
                    <behavior name="wcfFISH.DataServiceBehavior">
                        <serviceMetadata httpGetEnabled="false"/>
                        <serviceDebug includeExceptionDetailInFaults="false"/>
                    </behavior>
                </serviceBehaviors>
            </behaviors>
        </system.serviceModel>
    </configuration>
    
    1 回复  |  直到 16 年前
        1
  •  5
  •   Aaronaught    16 年前

    如果您总是想模拟客户端,那么对于所有操作,请将以下内容添加到 <behavior> 元素,即 <serviceMetadata> 元素:

    <serviceAuthorization impersonateCallerForAllOperations="true" />
    

    如果你想冒充

    最简单的解决方法就是

    另一种方法是打开ASP.NET兼容模式,将此添加到 <system.servicemodel> :

    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
    

    您有时还需要用以下内容来装饰您的服务:

    [AspNetCompatibilityRequirements(
        RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    

    但请记住,这将限制您利用许多其他较新WCF特性(如MSMQ或TCP绑定)的能力。我更喜欢应用程序池分离的方法。