2014-02-05 77 views
1

我想下面的app.config转换到VB.NET:转换的app.config代码在VB.NET或C#

<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中的答案是受欢迎的。

+0

您有什么样的问题?编译错误?功能错误? –

+0

功能,我不能让我的头绕着编程代码,xml看起来很简单。我只是不确定代码是用于XML中的上述功能的。我一直在努力工作几天。我有一个使用http或tcp的标准wcf接口。我正在尝试替换旧的.net远程示例,该示例使用rsa密钥进行身份验证,并找到以下页面:http://www.codeproject.com/Articles/166044/WCF-Client-Server-Application-with-Custom-Authenti - 现在我正在尝试使用没有app.config文件的工作。我从另一个应用程序传入URI。 –

+0

因此,不需要app.config,应用程序将被混淆,并为类名称等添加了保护,所以最好在应用程序 –

回答

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(); 
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() 

回答上述问题