B. Baker
B. Baker

Reputation: 13

Null Reference error from New-WebService cmdlet

I am trying to connect PowerShell to WCF service. I can connect to the service with a browser and with C#/.Net clients; however, when I try to connect using the PowerShell New-WebService cmdlet I get the following output:

PS Z:\> $SVC = New-WebServiceProxy -uri http://localhost:8733/Design_Time_Addresses/VIPService?Wsdl
New-WebServiceProxy : Object reference not set to an instance of an object.
At line:1 char:27
+ $SVC = New-WebServiceProxy <<<<  -uri http://localhost:8733/Design_Time_Addresses/VIPService?Wsdl
+ CategoryInfo          : NotSpecified: (:) [New-WebServiceProxy], NullReferenceException
+ FullyQualifiedErrorId : System.NullReferenceException,Microsoft.PowerShell.Commands.NewWebServiceProxy

Why am I getting a Null reference error?

Upvotes: 1

Views: 1308

Answers (1)

E.Z. Hart
E.Z. Hart

Reputation: 5747

I believe this is happening because New-WebServiceProxy only knows how to handle regular web services; it doesn't know about WCF bindings and such.

You can accomplish what you're trying to do using a client generated by svcutil.exe. Here's an example using the default WCF project that VS 2015 generates (which has a web service method called GetData which takes an int and returns a string). (This assumes your WCF service is running so that svcutil.exe can get the WSDL):

# Generate the proxy code
svcutil.exe http://localhost:55028/Service1.svc?wsdl

# Build the proxy DLL
csc /t:library .\Service1.cs

# Load the DLL
Add-Type -Path .\Service1.dll

$binding = new-object System.ServiceModel.BasicHttpBinding
$endpoint = new-object System.ServiceModel.EndpointAddress(“http://localhost:55028/Service1.svc")

# Create an instance of the client
$client =  New-Object Service1Client($binding, $endpoint)

# Call a method using your client
$client.GetData(4)

svcutil.exe is using the WSDL to generate a class called Service1.cs; we can use csc.exe (or VS, if you like) to compile the class into Service1.dll and load that into PowerShell using Add-Type. I'm using the BasicHttpBinding - you'll need to look at your configuration to see what the appropriate binding is for your service. Once you've got the binding and endpoint, you can create an instance of the client and start calling methods on it.

Upvotes: 2

Related Questions