c# - 如何通过成员(member)/授权在 Azure 上启用 WCF 服务的流传输?

标签 c# wcf azure wcf-binding wcf-security

我已经碰壁了,并且已经拉扯我的头发有一段时间了。基本上,我需要创建一个具有 ASP.NET 成员资格和授权提供程序的 WCF 服务,但它需要允许传输 byte[] 数组或 Stream 对象并将它们保存到 Azure。该服务本身托管在 Azure 上。

我遇到的问题是 WCF 需要消息层安全性来交换客户端凭据。所以我有以下配置,效果很好:

<system.serviceModel>
  <behaviors>
    <serviceBehaviors>
      <behavior name="DefaultServiceBehavior">
        <serviceMetadata httpsGetEnabled="true" />
        <serviceDebug includeExceptionDetailInFaults="true" />
        <serviceAuthorization principalPermissionMode="UseAspNetRoles" roleProviderName="SqlRoleProvider" />
          <serviceCredentials>
            <serviceCertificate x509FindType="FindBySubjectName" storeName="My" storeLocation="LocalMachine" findValue="SecureChannelCertificate" />
            <userNameAuthentication userNamePasswordValidationMode="MembershipProvider"  membershipProviderName="SqlMembershipProvider" />
         </serviceCredentials>
      </behavior>
    </serviceBehaviors>
  </behaviors>
  <bindings>
    <wsHttpBinding>
      <binding name="SecureBinding" messageEncoding="Mtom">
        <security mode="Message">
          <message clientCredentialType="UserName" negotiateServiceCredential="true" establishSecurityContext="true" />
        </security>
      </binding>
    </wsHttpBinding>
  </bindings>
</system.serviceModel>

然后需求发生了变化,现在我需要通过 WCF 服务将文件推送到 Azure。无论我做什么,WCF 都会向我发出各种各样的错误。

有谁知道如何配置该服务,以便它可以使用身份验证/授权以及流媒体?

谢谢!

最佳答案

虽然您提供的信息不足以确定问题,但我认为出现错误的原因是由于 WCF 默认消息大小限制,例如消息、内容和数组长度。

这些设置的默认值太低(maxReceivedMessageSize 为 64K、maxArrayLength 为 16K、maxStringContentLength 为 8K),无法通过 WCF 传输大数据,应增加默认值以能够处理包含较大数据的消息或字节数组。您可以使用 readerQuotas 更改这些默认值和 wsHttpBinding/binding元素。

以下是基于您的配置文件的示例设置,允许最多 4MB 消息、字符串和字节数组传输。

<system.serviceModel>
  <behaviors>
    <serviceBehaviors>
      <behavior name="DefaultServiceBehavior">
        <serviceMetadata httpsGetEnabled="true" />
        <serviceDebug includeExceptionDetailInFaults="true" />
        <serviceAuthorization principalPermissionMode="UseAspNetRoles" roleProviderName="SqlRoleProvider" />
          <serviceCredentials>
            <serviceCertificate x509FindType="FindBySubjectName" storeName="My" storeLocation="LocalMachine" findValue="SecureChannelCertificate" />
            <userNameAuthentication userNamePasswordValidationMode="MembershipProvider"  membershipProviderName="SqlMembershipProvider" />
         </serviceCredentials>
      </behavior>
    </serviceBehaviors>
  </behaviors>
  <bindings>
    <wsHttpBinding>
      <binding name="SecureBinding" messageEncoding="Mtom" maxReceivedMessageSize="4194304">
<readerQuotas maxStringContentLength="4194304" maxArrayLength="4194304"/>
        <security mode="Message">
          <message clientCredentialType="UserName" negotiateServiceCredential="true" establishSecurityContext="true" />
        </security>
      </binding>
    </wsHttpBinding>
  </bindings>
</system.serviceModel>

关于c# - 如何通过成员(member)/授权在 Azure 上启用 WCF 服务的流传输?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5035724/

相关文章:

azure - CPU 工作负载低时自动关闭 Azure VMSS 实例

c# - Entity Framework 5 Code First 自引用关系

c# - 没有配置的 ADO.NET Oracle.ManagedDataAccess.Client 的 Entity Framework 提供程序

c# - 如何使用C#发布的项目使SQLite数据库文件作为嵌入式资源工作

c# - 验证字符串是否对 IP 地址有效

.net - 如何在没有客户端证书的情况下出现 : WCF with Transport Security+Server Cert Auth.?

c# - WCF 客户端接收空响应值

c# - 尝试访问 iis 6.0 Web 服务时出现错误 403

.net - 部署面向 .NET Framework 4.6 的 Azure 云服务

azure - Azure 是否限制传出连接