将app.config转换为VB.NET或C#中的代码

时间:2014-02-05 14:57:06

标签: c# vb.net wcf

我正在尝试将以下app.config转换为VB.NET:

<configuration>
  <system.serviceModel>
    <extensions>
      <bindingElementExtensions>
        <add name="MessageEncoding" type="MessageEncodingElement, Server" />
      </bindingElementExtensions>
    </extensions>
    <bindings>
      <customBinding>
        <binding name="MessageEncoding">
          <MessageEncoding  contentEncryption="All" contentCompression="GZip" />
          <httpTransport/>
        </binding>
      </customBinding>
    </bindings>    
    <services>   
      <service name="ExampleService" behaviorConfiguration="Behavior">
        <endpoint address="http://localhost/Example" binding="customBinding" bindingConfiguration="MessageEncoding" contract="IExampleService" >
        </endpoint>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="Behavior">
          <serviceAuthenticationManager serviceAuthenticationManagerType="AuthenticationManager, Server"/>
          <serviceAuthorization principalPermissionMode="Custom" >
            <authorizationPolicies>
              <add policyType="AuthorizationPolicy, Server" />
            </authorizationPolicies>
          </serviceAuthorization>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

我尝试了以下内容,但我遗漏了一些内容:

Dim host As ServiceHost
Dim bindingElements As ICollection(Of BindingElement) = New List(Of BindingElement)()
Dim ServerMessageEncodingElement As New MessageEncodingBindingElement

ServerMessageEncodingElement.ContentEncryption = "All"
ServerMessageEncodingElement.ContentCompression = "GZip"

Dim httpBindingElement As New HttpTransportBindingElement()

bindingElements.Add(MessageEncodingElement)
bindingElements.Add(httpBindingElement)

Dim binding As New CustomBinding(bindingElements)
Dim endpoint As ServiceEndpoint = host.AddServiceEndpoint(GetType(IExampleService), binding, "http://example/Example")
Dim col = New ReadOnlyCollection(Of IAuthorizationPolicy)(New IAuthorizationPolicy() {New AuthenticationManager()})
Dim sa As ServiceAuthorizationBehavior = host.Description.Behaviors.Find(Of ServiceAuthorizationBehavior)()

If sa Is Nothing Then
    sa = New ServiceAuthorizationBehavior()
    host.Description.Behaviors.Add(sa)
End If

sa.ExternalAuthorizationPolicies = col

我已尝试过上述的多个版本,如果需要,我可以进行其他尝试。我只需要把它拿出来。欢迎使用C#或VB.NET中的答案。

2 个答案:

答案 0 :(得分:0)

dim host As New ServiceHost(GetType(ExampleService))
     Dim bindingElements As ICollection(Of BindingElement) = New List(Of BindingElement)()
        Dim MessageEncodingElement As New MessageEncodingBindingElement()
        ServerMessageEncodingElement.ContentEncryption = ContentEncryptionType.All
        ServerMessageEncodingElement.ContentCompression = ContentCompressionType.None
        Dim httpBindingElement As New HttpTransportBindingElement()
        bindingElements.Add(MessageEncodingElement)
        bindingElements.Add(httpBindingElement)
        Dim binding As New CustomBinding(bindingElements)
        Dim endpoint As ServiceEndpoint = host.AddServiceEndpoint(GetType(IExampleService), binding, "http://" & My.Computer.Name & "/Example")
        Dim col = New ReadOnlyCollection(Of IAuthorizationPolicy)(New IAuthorizationPolicy() {New AuthorizationPolicy()})
        Dim sa As ServiceAuthorizationBehavior = host.Description.Behaviors.Find(Of ServiceAuthorizationBehavior)()
        sa.ExternalAuthorizationPolicies = col
        sa.PrincipalPermissionMode = PrincipalPermissionMode.Custom
        Dim sm As ServiceAuthenticationBehavior = host.Description.Behaviors.Find(Of ServiceAuthenticationBehavior)()
        sm.ServiceAuthenticationManager = New AuthenticationManager
        host.Open()

回答上述问题

答案 1 :(得分:0)

ServiceHost host = new ServiceHost(typeof(ExampleService));
ICollection<BindingElement> bindingElements = new List<BindingElement>();
ServerMessageEncodingBindingElement ServerMessageEncodingElement = new ServerMessageEncodingBindingElement();
ServerMessageEncodingElement.ContentEncryption = ContentEncryptionType.All;
ServerMessageEncodingElement.ContentCompression = ContentCompressionType.None;
HttpTransportBindingElement httpBindingElement = new HttpTransportBindingElement();
bindingElements.Add(ServerMessageEncodingElement);
bindingElements.Add(httpBindingElement);
CustomBinding binding = new CustomBinding(bindingElements);
ServiceEndpoint endpoint = host.AddServiceEndpoint(typeof(IExampleService), binding, "http://" + My.Computer.Name + "/Example");
dynamic col = new ReadOnlyCollection<IAuthorizationPolicy>(new IAuthorizationPolicy[] { new ChallengeAuthorizationPolicy() });
ServiceAuthorizationBehavior sa = host.Description.Behaviors.Find<ServiceAuthorizationBehavior>();
sa.ExternalAuthorizationPolicies = col;
sa.PrincipalPermissionMode = PrincipalPermissionMode.Custom;
ServiceAuthenticationBehavior sm = host.Description.Behaviors.Find<ServiceAuthenticationBehavior>();
sm.ServiceAuthenticationManager = new ChallengeAuthenticationManager();
host.Open();