# New to objects, trying to convert C#

**URL:** <https://forums.powershell.org/t/new-to-objects-trying-to-convert-c/17367>\
**Category:** PowerShell Help\
**Created:** [August 21, 2021, 3:23am UTC](https://forums.powershell.org/t/new-to-objects-trying-to-convert-c/17367 "2021-08-21T03:23:20Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![flenwan](https://avatars.discourse-cdn.com/v4/letter/f/b782af/32.png) [@flenwan](https://forums.powershell.org/u/flenwan)\
**Post date:** [August 21, 2021, 3:23am UTC](https://forums.powershell.org/t/new-to-objects-trying-to-convert-c/17367/1 "2021-08-21T03:23:20Z")

</div>

Still learning PS and struggling with objects. I have sample C# code and can not properly implement it in PS.

```auto
//Instantiate the web service as an object.
Service1.Service1 obj = new Service1.Service1();
//Instantiate the authentication as an object.
Service1.ValidationSoapHeader authentication = new
Service1.ValidationSoapHeader();
//Assign a DevToken value to the authentication object.
authentication.DevToken = token
obj.ValidationSoapHeaderValue = authentication;
//Build the XML Input String
xmlInput = somexml
{
//Send the XML Input to the MWS
serviceOutput = obj.OrderList(xmlInput);

Here is where I am at with the PS. Obviously this does not work...

$obj = New-WebServiceProxy -Uri $URI -Namespace "WSProxy"-Class "Midnight"
$authentication = New-Object ("WSProxy.ValidationSoapHeader")

$authentication.DevToken = $token
$obj.ValidationSoapHeaderValue = $authentication

# This line works
$obj | Get-Member -MemberType method

$output = $obj.OrderList($inputXML)

Exception calling "OrderList" with "1" argument(s): "System.Web.Services.Protocols.SoapException: Server 
request. ---> System.InvalidOperationException: The ConnectionString property has not been initialized.

```

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [August 21, 2021, 11:28am UTC](https://forums.powershell.org/t/new-to-objects-trying-to-convert-c/17367/2 "2021-08-21T11:28:52Z")

</div>

Hi, welcome to the forum 👋

I’m not sure there’s enough information here for people to help you.

The `OrderList` method will be specific to the object referenced by `$obj` which is being returned by whichever service you’re accessing. If it’s a public service or a service that publishes their documentation can you share a link?

How are you setting the `$inputXML` variable and what is its value?

---

<div class="post-metadata">

**Author:** ![flenwan](https://avatars.discourse-cdn.com/v4/letter/f/b782af/32.png) [@flenwan](https://forums.powershell.org/u/flenwan)\
**Post date:** [August 21, 2021, 2:58pm UTC](https://forums.powershell.org/t/new-to-objects-trying-to-convert-c/17367/3 "2021-08-21T14:58:22Z")

</div>

Thanks for the reply. I thought my problem was just on how the objects were being built, specifically the authentication part. Did I translate the C# example to PS correctly? If so, I am proud! Here is the rest of the code and resources, hope this helps.

> **[MidnightWebService\_MethodDefinitions.docx](https://drive.google.com/file/d/1YEIF8Jxqo1EHYi1ZAvqC1lAYNnkFGDT0/view)**
>
> Google Drive file.

```auto
$inputXML = "<OrderList>" + "<InputParameter>" + "<OrderNumber>" + "1528" + "</OrderNumber>" + "<CustomerCode></CustomerCode>" + "<CustomerID></CustomerID>" + "<OrderDateFrom></OrderDateFrom>" + "<OrderDateTo></OrderDateTo>" + "<OrderStatus></OrderStatus>" + "</InputParameter>" + "</OrderList>"
$URI = "http://ws.vsmidnight.com/Service1.asmx?WSDL"

$obj = New-WebServiceProxy -Uri $URI -Namespace "WSProxy"-Class "Midnight"
$authentication = New-Object ("WSProxy.ValidationSoapHeader")

$authentication.DevToken = $token
$obj.ValidationSoapHeaderValue = $authentication

# This line works
$obj | Get-Member -MemberType method

$output = $obj.OrderList($inputXML)

```

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [August 21, 2021, 3:53pm UTC](https://forums.powershell.org/t/new-to-objects-trying-to-convert-c/17367/4 "2021-08-21T15:53:04Z")

</div>

Your conversion looks OK to me 👍

I can replicate the error after adding the `$authentication` object to the `ValidationSoapHeaderValue` field. I’m using a made up value for the token but I get a different error when it’s not present.

As the object seems to be created properly, I wonder if you just have an authentication problem.

---

<div class="post-metadata">

**Author:** ![flenwan](https://avatars.discourse-cdn.com/v4/letter/f/b782af/32.png) [@flenwan](https://forums.powershell.org/u/flenwan)\
**Post date:** [August 21, 2021, 4:09pm UTC](https://forums.powershell.org/t/new-to-objects-trying-to-convert-c/17367/5 "2021-08-21T16:09:42Z")

</div>

I’m quite happy I at least got that part right! Cool! I am getting help from another person who has provided a different approach but I am getting a different error.

```auto
Function GetOrderIDfromOrderNumber() {
    [CmdletBinding()]
    param (
    [Parameter()]
    [string]
    $OrderNum
    )
    $devToken = "removed"
    $soapUri = "http://ws.vsmidnight.com/Service1.asmx?WSDL"
    $midNightWS = New-WebServiceProxy -Uri $soapUri -Namespace WebServiceProxy -Class Service1
    $midNightWSAuth = New-WebServiceProxy -Uri $soapUri -Namespace WebServiceProxy -Class ValidationSoapHeader
    $midNightWSAuth.DevToken = $devToken
    $midNightWS.ValidationSoapHeaderValue = $midNightWSAuth
    $xmlInput = "<OrderList>" +
    "<InputParameter>" +
        "<OrderNumber>" + $OrderNum + "</OrderNumber>" +
        "<CustomerCode></CustomerCode>" +
        "<CustomerID></CustomerID>" +
        "<OrderDateFrom></OrderDateFrom>" +
        "<OrderDateTo></OrderDateTo>" +
        "<OrderStatus></OrderStatus>" +
    "</InputParameter>" +
    "</OrderList>"
    $midNightWS.OrderList($xmlInput)
  }
  # Replace "ASDASD" with a valid order number
  $soapOutput = GetOrderIDfromOrderNumber("1528")
  
  
  New-WebServiceProxy : Could not load file or assembly 
'file:///C:\Users\jflener\AppData\Local\Temp\megcv3uz.dll' or one of its dependencies. The system       
cannot find the file specified.
At C:\Users\jflener\Downloads\soapApi.ps1:10 char:19
+ ... idNightWS = New-WebServiceProxy -Uri $soapUri -Namespace WebServicePr ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [New-WebServiceProxy], FileNotFoundException
    + FullyQualifiedErrorId : System.IO.FileNotFoundException,Microsoft.PowerShell.Commands.NewWebServi 
   ceProxy

New-WebServiceProxy : Could not load file or assembly 
'file:///C:\Users\jflener\AppData\Local\Temp\ez3qhm4k.dll' or one of its dependencies. The system 
cannot find the file specified.

```

---

<div class="post-metadata">

**Author:** ![flenwan](https://avatars.discourse-cdn.com/v4/letter/f/b782af/32.png) [@flenwan](https://forums.powershell.org/u/flenwan)\
**Post date:** [August 21, 2021, 4:13pm UTC](https://forums.powershell.org/t/new-to-objects-trying-to-convert-c/17367/6 "2021-08-21T16:13:24Z")

</div>

Also in that error message the dll is a random name each time I run

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [August 21, 2021, 8:56pm UTC](https://forums.powershell.org/t/new-to-objects-trying-to-convert-c/17367/7 "2021-08-21T20:56:24Z")

</div>

The class isn’t `Service1` it’s `Midnight`. That should get rid of the error from the first `New-WebServiceProxy`. I’m not sure why you’d create a second one for auth or what the class would be if you do that.

Have you been able to confirm that your token works?

A couple of other suggestions. Instead of sending a `String` try an `XML` object. I don’t think that’s required from the code sample, but it’s worth a try. I’d change your code to use a Here String rather than contatenation.

```auto
[XML]$xmlInput = @"
<OrderList>
    <InputParameter>
        <OrderNumber> $OrderNum </OrderNumber>
        <CustomerCode></CustomerCode>
        <CustomerID></CustomerID>
        <OrderDateFrom></OrderDateFrom>
        <OrderDateTo></OrderDateTo>
        <OrderStatus></OrderStatus>
    </InputParameter>
</OrderList>
"@

```

The other option worth testing would be to build what you’re sending using the examples for PHP or VBA and try sending it with `Invoke-WebRequest` or `Invoke-RestMethod`. Here’s an example:

> **[Invoke-RestMethod with a SOAP API - tommymaynard.com](https://tommymaynard.com/invoke-restmethod-with-a-soap-api/)**
>
> Did it again. I’m back here this evening to write another post after authoring some PoC code for a project. I’ve got to put it somewhere for later. In doing that, there’s no reason it shouldn’t be content in which others can consume. In the past, I...

---

<div class="post-metadata">

**Author:** ![flenwan](https://avatars.discourse-cdn.com/v4/letter/f/b782af/32.png) [@flenwan](https://forums.powershell.org/u/flenwan)\
**Post date:** [August 21, 2021, 9:29pm UTC](https://forums.powershell.org/t/new-to-objects-trying-to-convert-c/17367/8 "2021-08-21T21:29:04Z")

</div>

I got a little bit further by changing up a couple lines and got rid of the first error. I’m really not sure what is going on as this is now above my understanding but hopefully this helps. I hope I can get this method working as it looks better but I will try the PHP method too.

```auto
    $midNightWS = New-WebServiceProxy -Uri $soapUri -Class Midnight
    $midNightWSAuth = [Microsoft.PowerShell.Commands.NewWebserviceProxy.AutogeneratedTypes.WebServiceProxy2idnight_com_Service1_asmx.ValidationSoapHeader]::new()

Unable to find type [Microsoft.PowerShell.Commands.NewWebserviceProxy.AutogeneratedTypes.WebServiceProxy2idnight_com_Service1_asmx.ValidationSoapHeader].
At T:\Automation Workspace\midnight\soap.ps1:14 char:23
+ ... ghtWSAuth = [Microsoft.PowerShell.Commands.NewWebserviceProxy.Autogen ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (Microsoft.Power...ationSoapHeader:TypeName) [], RuntimeException
    + FullyQualifiedErrorId : TypeNotFound

```

I tried that without the -Class and with Midnight as the name and both return same error

I also try this way and get another error.

```auto
$soapUri = "http://ws.vsmidnight.com/Service1.asmx"
    $midNightWS = New-WebServiceProxy -Uri $soapUri -Class Midnight
    $midNightWSAuth = New-WebServiceProxy -Uri $soapUri -Class ValidationSoapHeader
    $midNightWSAuth.DevToken = $devToken

The property 'DevToken' cannot be found on this object. Verify that the property exists and can be set.
At T:\Automation Workspace\midnight\soap.ps1:15 char:5
+ $midNightWSAuth.DevToken = $devToken
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyAssignmentException
 
Exception setting "ValidationSoapHeaderValue": "Cannot convert the 
"Microsoft.PowerShell.Commands.NewWebserviceProxy.AutogeneratedTypes.WebServiceProxy1s_vsmidnight_com_Service1_asmx.Service1" value of type 
"Microsoft.PowerShell.Commands.NewWebserviceProxy.AutogeneratedTypes.WebServiceProxy1s_vsmidnight_com_Service1_asmx.Service1" to type       
"Microsoft.PowerShell.Commands.NewWebserviceProxy.AutogeneratedTypes.WebServiceProxy1s_vsmidnight_com_Service1_asmx.ValidationSoapHeader"." 
At T:\Automation Workspace\midnight\soap.ps1:16 char:5
+ $midNightWS.ValidationSoapHeaderValue = $midNightWSAuth
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [], SetValueInvocationException
    + FullyQualifiedErrorId : ExceptionWhenSetting

```

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [August 22, 2021, 11:39am UTC](https://forums.powershell.org/t/new-to-objects-trying-to-convert-c/17367/9 "2021-08-22T11:39:46Z")

</div>

I’m not sure there was anything wrong with your first solution. It would appear that everything is being created properly in that solution but there’s a possible problem with the token.

I think I’ve managed to put together the correct code to test with `Invoke-WebRequest` can you give this a try with your token? It seems to give a more verbose error message (see below).

```auto
$URI = "http://ws.vsmidnight.com/Service1.asmx"
   
$header = @{"Content-Type" = "text/xml"}

$body = @'
<?xml version='1.0' encoding='utf-8'?>
<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:vsm='http://vsmidnight.com/'>
    <soapenv:Header>
    <vsm:ValidationSoapHeader>
        <vsm:DevToken>YourTokenString</vsm:DevToken>
    </vsm:ValidationSoapHeader>
    </soapenv:Header>
    <soapenv:Body>
    <vsm:OrderList>
        <vsm:inputXML><![CDATA[<OrderList>
            <InputParameter>
            <OrderNumber>1528</OrderNumber>
            <CustomerCode></CustomerCode>
            <CustomerID></CustomerID>
            <OrderDateFrom></OrderDateFrom>
            <OrderDateTo></OrderDateTo>
            <OrderStatus></OrderStatus>
            </InputParameter>
            </OrderList>]]></vsm:inputXML>
        </vsm:OrderList>
    </soapenv:Body>
</soapenv:Envelope>
'@

Invoke-WebRequest -Uri $URI -Method Post -Header $header -Body $body

```

✂

```auto
The ConnectionString property has not been initialized.
   at System.Data.SqlClient.SqlConnection.PermissionDemand()

```

✂

---

<div class="post-metadata">

**Author:** ![flenwan](https://avatars.discourse-cdn.com/v4/letter/f/b782af/32.png) [@flenwan](https://forums.powershell.org/u/flenwan)\
**Post date:** [August 22, 2021, 3:19pm UTC](https://forums.powershell.org/t/new-to-objects-trying-to-convert-c/17367/10 "2021-08-22T15:19:59Z")

</div>

Thank you, this one works. And I remember now I saw that error before and did not realize it was a credential error. I much to do today now to try and wrap my head around all of this.

---

<div class="post-metadata">

**Author:** ![flenwan](https://avatars.discourse-cdn.com/v4/letter/f/b782af/32.png) [@flenwan](https://forums.powershell.org/u/flenwan)\
**Post date:** [August 22, 2021, 3:32pm UTC](https://forums.powershell.org/t/new-to-objects-trying-to-convert-c/17367/11 "2021-08-22T15:32:03Z")

</div>

Would you believe my token had a $23 in the middle of it? I’m so embarrassed right now…

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [August 22, 2021, 9:45pm UTC](https://forums.powershell.org/t/new-to-objects-trying-to-convert-c/17367/12 "2021-08-22T21:45:30Z")

</div>

Glad to hear it’s working for you. Have you tried your original code with the correct token? `New-WebServiceproxy` is the preferred way of doing things.

---

<div class="post-metadata">

**Author:** ![flenwan](https://avatars.discourse-cdn.com/v4/letter/f/b782af/32.png) [@flenwan](https://forums.powershell.org/u/flenwan)\
**Post date:** [August 22, 2021, 10:41pm UTC](https://forums.powershell.org/t/new-to-objects-trying-to-convert-c/17367/13 "2021-08-22T22:41:22Z")

</div>

Yes, it works with my original code. Thank you for your help, it was really an extra effort you put in to help me and I appreciate it.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:21pm UTC](https://forums.powershell.org/t/new-to-objects-trying-to-convert-c/17367/14 "2024-05-16T20:21:59Z")

</div>


