tirsdag den 22. marts 2011

Calling WCF Web Service from DLL

I need to call a webservice from a DLL ( powershell PSCmdlet ) but when testing it I keept getting below error

PS C:\Users\administrator.INT> Get-CustomerServiceObject -CustomerServiceID 1880
Get-CustomerServiceObject : Could not find default endpoint element that references contract 'webserviceCustomerService
s.CustomerServices' in the ServiceModel client configuration section. This might be because no configuration file was f
ound for your application, or because no endpoint element matching this contract could be found in the client element.
At line:1 char:26
+ Get-CustomerServiceObject <<<<  -CustomerServiceID 1880
    + CategoryInfo          : NotSpecified: (:) [Get-CustomerServiceObject], InvalidOperationException
    + FullyQualifiedErrorId : System.InvalidOperationException,Cloud.Provisioning.CloudSnapin.Cloud.Provisioning.GetCu
   stomerServiceObject

PS C:\Users\administrator.INT>

One way to fix this would be to add the system.serviceModel from app.config to powershell’s .config file,but that just sound wrong in my ears. Google !

So found this post and re-wrote it to my own VB.NET version

'Dim wsCustomerServices As New webserviceCustomerServices.CustomerServicesClient
Dim wsCustomerServices As webserviceCustomerServices.CustomerServices
Try
    Dim asm As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly
    Dim assemName As System.Reflection.AssemblyName = asm.GetName()
    Dim dllPath As String = asm.CodeBase.Replace("file:///", "")
    dllPath = IO.Path.GetDirectoryName(dllPath)

    Dim stockConfiguration As Configuration = ConfigurationManager.OpenMappedExeConfiguration(New ExeConfigurationFileMap() With {.ExeConfigFilename = (dllPath & "\Cloud.Provisioning.CloudSnapin.dll.config")}, ConfigurationUserLevel.None)
    Dim stockChannelFactory As ConfigurationChannelFactory(Of webserviceCustomerServices.CustomerServices)
    stockChannelFactory = New ConfigurationChannelFactory(Of webserviceCustomerServices.CustomerServices)("CustomBinding_CustomerServices", stockConfiguration, Nothing)
    wsCustomerServices = stockChannelFactory.CreateChannel()

Catch ex As Exception
    Throw ex
End Try

Ingen kommentarer:

Tilføj en kommentar