How to consume a web service in code directly

As well as using a wizard it is possible to access web services using code. These are the steps.

  1. Open a Visual Studio Command Prompt
  2. Navigate to a suitable directory e.g. c:\Temp
  3. Use WSDL. Examples
    C# is default language
    wsdl http: //  …myservice.asmx
    For VB use
    wsdl /language:vb http://.. myservice.asmx
    Your service might be .svc
  4. Press return and the wsdl program should write out a file for your consumption
  5. This creates a web service proxy, which will talk to the service.
  6. In your project create a folder say named Service
  7. Add in your new file
  8. Add reference to System.Web.Services
  9. Add code similar to:

myService MyService1 = new myService()

var result = myService.myMethod();

To create a batch file to do this automatically

Thanks to:

http://stackoverflow.com/questions/5879076/how-to-create-a-batch-file-for-visual-studio-command-prompt

call “C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat” x86
wsdl /language:vb /out:MyServiceName.vb http:// … myservice.svc
pause

Why does the consumed service return arrays when I specified a List(of T)

WCF tries hard to not create services which are .Net dependent. So by default the return of List(Of T) will return an array.

If you add a service using the .Net UI, then there is an option:

Configure Service Reference > Always Generate Message Contracts > Collection Type > Change to what you want, which may be a System.Collections.Generic.List.

There is a similar option with svcutil and probably wsdl, but I have not used this yet.

The End.

Advertisements

Using System.Linq.Dynamic in VB.Net

Dynamic Linq

Was trying to using dynamic filter as I would do with datasets. Sometimes you may want to build the query at runtime. Steps are:

  1. Use NuGet to install System.Linq.Dynamic
  2. At top of class Imports System.Linq.Dynamic
  3. Use code as in example below:
Dim queryString1 = "Title = @0"
FilmsList = FilmsList.AsQueryable.Where(queryString1, "Casablanca").ToList

Dim queryString2 = "Title.Contains(@0)"
FilmsList = FilmsList.AsQueryable.Where(queryString2, "a").ToList

Dim queryString3 = "Title = ""Casablanca"""
FilmsList = FilmsList.AsQueryable.Where(queryString3).ToList
FilmDataGrid.ItemsSource = FilmsList

Error 1: Character literal must contain exactly one character

Solution 1: Use double quotes when constructing predicate in code e.g. Dim myFilter = String.Concat( “Title = “””,myTitle,””””)

End.

WPF DataGrid Get Current Row

Data Grid – Get Current Row. E.g. For Jump to Details – Data Grid Button Click

<DataGrid.Columns>
<DataGridTemplateColumn Header="Details" Width="80">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button x:Name="JumpButtonColumn" Content="{Binding Path=AdviceNoteNo}" Click="JumpButtonColumn_Click"></Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>>

And the Code

private void JumpButtonColumn_Click(object sender, RoutedEventArgs e)
{
if (invoiceDataGrid.CurrentColumn != null && invoiceDataGrid.CurrentColumn.DisplayIndex == 0)
{
Invoice myInvoice = ((FrameworkElement)sender).DataContext as Invoice;
Jump(myInvoice.InvoiceNo);
}
};

or

Private Sub JumpButtonColumn_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
   If AdviceNoteDataGrid.CurrentColumn IsNot Nothing AndAlso AdviceNoteDataGrid.CurrentColumn.DisplayIndex = 0 Then
       Dim myAdviceNote As AdviceNote = CType((CType(sender, FrameworkElement)).DataContext, AdviceNote)
       Jump(myAdviceNote.AdviceNoteNo)
   End If
End Sub

End