WCF 项目库

Posted

技术标签:

【中文标题】WCF 项目库【英文标题】:WCF Project library 【发布时间】:2013-05-26 06:49:18 【问题描述】:

我想将我的 WCF 服务从 WebSite App_Code 文件夹迁移到项目库。 据我所知,WCF 库能够读取有关服务模型的 Web 配置,所以我所做的唯一操作是: 1 - 创建新的项目库并将所有关于 wcf 的代码从 app_code 放入其中。 2 - 修改 web 配置以指向具有完整限定名的服务类(命名空间 + 类名) 3 - 修改 svc 文件以指向具有完整限定名称的服务类实例。

但是,我的服务不再运行。我正在使用带有自定义验证器的 ws-httpbinding,但似乎我的服务需要一个基本的 http 绑定。 我正在努力解决的错误如下所示: 消息的请求必须受到保护,例如合约操作要求('IMyService','http://tempuri.org/')。保护必须通过 ('BasicHttpBinding','http://tempuri.org/') 绑定来实现。

@@EDIT:

我的 Web.Config:

  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="MyBehavior">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
          <serviceCredentials>
            <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="MyWcfNamespaceOfMyDLL.MyCustomValidator" />
            <serviceCertificate findValue="localhost" x509FindType="FindBySubjectName" storeLocation="LocalMachine" storeName="My" />
          </serviceCredentials>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <bindings>
      <wsHttpBinding>
        <binding name="MyBinding" maxBufferPoolSize="1000000000" maxReceivedMessageSize="1000000000" messageEncoding="Mtom">
          <security mode="Message">
            <message clientCredentialType="UserName" />
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>
    <services>
      <service behaviorConfiguration="MyBehavior" name="MyServiceName">
        <endpoint address="" binding="wsHttpBinding" contract="MyWcfNamespaceOfMyDLL.IMyServiceName" bindingConfiguration="MyBinding">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
    </services>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
</configuration>

这是我在网站根目录中的 svc 文件:

<%@ ServiceHost Language="C#" Debug="true" Service="MyWcfNamespaceOfMyDLL.MyServiceName" %>

最后,我的 dll 中的服务合同如下所示:

[ServiceContract]
public interface IMyService

    [OperationContract(ProtectionLevel=System.Net.Security.ProtectionLevel.EncryptAndSign)]
    string DoSomething();


public class MyServiceName : IMyService

   public string DoSomething();


public class MyValidator : UserNamePasswordValidator
    
// simple validation

有什么想法吗?

【问题讨论】:

应该可能会显示您的 web.config 代码。 您应该发布您的代码 - 配置的服务模型部分和服务合同。 刚刚编辑了所有内容.. 您可能对服务合同的默认命名空间有问题 - tempuri.org。 我不知道。你有什么建议我解决这个问题吗? 【参考方案1】:

我已经解决了。 问题在于网络配置中缺少 dll 名称。

我不得不改变我的配置如下:

第一:

<userNameAuthentication userNamePasswordValidationMode="Custom" 
      customUserNamePasswordValidatorType="="MyWcfNamespaceOfMyDLL.MyCustomValidator", MyDllName" />

第二:

<services>
      <service behaviorConfiguration="MyBehavior" 
               name="MyWcfNamespaceOfMyDLL.MyServiceName">
        <endpoint address="" binding="wsHttpBinding"
                  contract="IMyServiceName" bindingConfiguration="MyBinding">
          <identity>

我认为每个人都可以开始疯狂..!!

嗯,如你所见,我必须:

在 userNameAuthentication 标记上指定 DLL 名称。 在服务标签的属性名称上指定命名空间名称。 删除端点标签的合约属性上有关命名空间名称的规范。

好的我已经解决了,但是我有点担心下一个未来..!!

【讨论】:

【参考方案2】:

您确定问题来自服务而不是来自客户端

您能告诉我们您如何调用此服务吗?

消息的请求必须受到保护,例如合约操作要求('IMyService','http://tempuri.org/')。这 保护必须通过 ('BasicHttpBinding','http://tempuri.org/') 绑定。

正如here所说,

如果绑定不支持安全性(如 BasicHttpBinding), 有效的 System.Net.Security.ProtectionLevel 是 整个合同的 ProtectionLevel.None。结果是 根据端点绑定,客户端可能需要不同的 消息或传输级别的安全保护,即使合约 指定 ProtectionLevel.None。

此错误消息表明您正在使用没有 ProtectionLevel 的 basicHttpBinding 调用服务。

此外,Web.config IMyServiceName 中的合同与代码“IMyService”或错误消息中的合同不同。

【讨论】:

IMyServiceName 和 IMyService 只是我输入的错误。是的,我正在与您打交道,但我无法从任何客户端添加服务参考。另外,我会记得我在将服务代码移动到 dll 项目后遇到了这个问题。当我的服务在 asp.net 中完成时,一切正常。我正在使用服务器证书进行保护,但我再次重复,如果我撤消所有修改并将我的服务接口放在 app_code 中,一切正常。

以上是关于WCF 项目库的主要内容,如果未能解决你的问题,请参考以下文章

如何通过wcf服务更新宿主项目中的winform控件?

在 WCF 项目中设置 Web API

SVC 文件和 WCF 项目之间的关系?

WCF入门-项目间调用服务

使用单个 Windows 服务托管 4 个不同的 WCF 项目。如何?

在另一个项目 c# 中使用引用项目的 WCF 服务