![]() |
#1 |
Участник
|
You should read the post about connecting to NAV Web Services from C# using Service Reference (config file version) before continuing here.
Code is king As you saw in the other post, the config file was pretty complicated and although it is editable by hand and as such could be modified at installtime or whatever, I actually prefer to capture a number of these settings in code and only have very specific values in a config file (like f.ex. the base URL). In NAV you would never have the full Service URL on all services in a config file. This would mean that you would have to change company in a number of locations in a config file – that just doesn’t fly. If we have a look at the config file once more, you will see that there is a Binding configuration, specifying a BasicHttpBinding with a couple of settings. If we want to create this binding in code, it would look like: // Create a NAV comatible binding BasicHttpBinding navWSBinding = new BasicHttpBinding(); navWSBinding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly; navWSBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows; Having this binding class, we can now create a systemService Service Client simply by: SystemService_PortClient systemService = new SystemService_PortClient(navWSBinding, new EndpointAddress(baseURL + "SystemService")); Specifying the endpoint address to the constructor. The only other thing we need to specify is the endpoint behavior to allow delegation. This is done in code by: systemService.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Delegation; systemService.ClientCredentials.Windows.AllowNtlm = true; Using code like this actually makes the app.config obsolete and you can delete the config file totally when running the below application. The entire application The following is a listing of the full console application using code to set all properties and no app.config is necessary (nor used at all): using System; using System.Net; using testAppWCF.SystemServiceRef; using testAppWCF.CustomerPageRef; using System.ServiceModel; namespace testAppWCF { class Program { static void Main(string[] args) { string baseURL = "
__________________
Расскажите о новых и интересных блогах по Microsoft Dynamics, напишите личное сообщение администратору. |
|