Multiple base addresses for WCF Service

If we do not want to explicitly set address for each EndPoint in our service then we define BASE ADDRESS for the EndPoint exposed for our service

clip_image002

And we use it in service configuration as below,

clip_image004

The advantage of having base address is that, if we are changing the service URL, we need to change at only one place instead of going and changing address of each endpoint.

WCF allow us to give multiple base addresses for each type of protocol. And at the run time corresponding endpoint will take the base address.

clip_image006

In above configuration, we added one more base address using net.tcp protocol.

Now at endpoint configuration, we will configure it as below

clip_image008

At the run time WCF automatically will map base address to corresponding endpoint with matched protocol.

For reference full Web.Config will look like


<system.serviceModel>
 <behaviors>
 <serviceBehaviors>
 <behavior name="MyBeh">
 <serviceMetadata httpGetEnabled="true"/>
 <serviceDebug includeExceptionDetailInFaults="false"/>
 </behavior>
 </serviceBehaviors>
 </behaviors>
 <services>
 <service name="WcfService1.Service1"  behaviorConfiguration="MyBeh">
 <endpoint address=""
 binding="basicHttpBinding"
 contract="WcfService1.IService1"/>
 <endpoint address="nete"
 binding ="netTcpBinding"
 contract ="WcfService1.IService1"/>
 <endpoint address="mex"
 binding="mexHttpBinding"
 contract="IMetadataExchange"/>
 <endpoint address="mex1"
 binding="mexTcpBinding"
 contract="IMetadataExchange"/>
 <host>
 <baseAddresses>
 <add baseAddress="<a href="http://localhost:8181/Service1.svc/&quot;/">http://localhost:8181/Service1.svc/"/</a>>
 <add baseAddress="net.tcp://localhost:9876/Service1.svc/"/>
 </baseAddresses>
 </host>
 </service>
 </services>
 </system.serviceModel>

 

Enabling JavaScript IntelliSense on Visual Web Part in SharePoint 2010

Open Visual Studio 2010 and create a Visual Web Part project as selecting from SharePoint 2010 tab

clip_image002

Now provide name of the site where you want to deploy the web part

clip_image003

Click on Validate button to validate the site. If you are able to connect and get below message then we are we are good to go.

clip_image004

Open VisualWebPart1UserControl.ascx and add below script

clip_image006

After adding above script add

clip_image007

Now add below script,

clip_image009

Inside script tag , write a function

clip_image011

And now u can see that in this function IntelliSense is working

clip_image001

So in JavaScript function it would be working like below

clip_image013

Walkthrough creating REST Service in WCF 4.0

Objective

This article will explain step to step explanation of, how to create a REST based service and how to consume that in a managed client.

Step 1`

Create a new project as WCF Service application type.

clip_image002

Step 2

Delete all the default code from IService.cs and Service.svc.cs

Now open Web.Config file and delete <system.serviceModel>

If you are using VS2010 then delete below code from Web.Config

clip_image004

Step 3

Right click on Service.svc and open View markup.

clip_image005

In Markup of RestService.cs , add below code there

Factory=”System.ServiceModel.Activation.WebServiceHostFactory”

So after adding code the markup would look like

Markup of RestService.cs

clip_image007

Step 4

Add following references to the WCF Service Application project, if you are using VS2010

Microsoft.Http.dll

Microsoft.Http.Extension.dll

System.ServiceModel.Web.dll

Step 5

Add a new project as of type class library and give it name UtilityClasses.

clip_image009

Add a class Number to this class library.

Number.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace UtilityClasses
{
 [Serializable]
 public class NumberService
 {
 public int Number1 { get; set; }
 public int Number2 { get; set; }
 }
}

 

Step 6

Define the Service Contract as below,

IService1.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Web;
using UtilityClasses;

namespace WcfService3
{

 [ServiceContract]
 public interface IService1
 {
 [OperationContract(Name="AddParameter")]
 [WebInvoke(UriTemplate = "/Add/MyAdd",Method="POST")]
 int  Add(NumberService n1);
 [OperationContract(Name = "Add")]
 [WebGet(UriTemplate = "/")]
 int Add();

}

}

 

This code is used to construct URI for REST service.

clip_image010

Method parameter says what type of HTTP request; this URI is going to entertain. In this case it is Http POST.

UriTemplate parameter says, what would be URI for this particular method. In this case it is one back slash means; it is root URI of this service.

So, to invoke this method add (), the URI address would be

http://localhost:3602/RestService.svc/

Where 3602 is port number of web server, where this service is running.

Let, if the above code is modified as

clip_image012

Then it would be invoked as

http://localhost:3602/RestService.svc/ADD/MyAdd

Step 7

Now we need to implement the service

Service1.svc.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using UtilityClasses;

namespace WcfService3
{

 public class Service1 : IService1
 {
 public int res = 100;

public int Add(NumberService n1)
 {

res = Convert.ToInt32(n1.Number1) + Convert.ToInt32(n1.Number2);
 return res;
 }
 public int Add()
 {
 return res;
 }
 }
}

&nbsp;

 

Step 8

Test the Service in Browser

1. Build the Service

2. Right click on Service1.svc and select view in browser.

clip_image014

Output 100 is returned in the browser by the service. It is because, below service method. URI of below GET method is mapped to root of the URI. And this method is returning 100.

clip_image015

Step 9

Consume the service

Add new console project in solution and give it name as ConsoleTestProject.

clip_image017

Add reference of project UtilityClasses Add following references to the console project.

Microsoft.Http.dll

Microsoft.Http.Extension.dll

System.ServiceModel.Web.dll

Step 10

Program.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using Microsoft.Http;
using Microsoft.ServiceModel.Web;
using Microsoft.ServiceModel.Web.SpecializedServices;
using System.Runtime.Serialization;
using UtilityClasses;

namespace ConsoleTestProject
{
 class Program
 {
 static void Main(string[] args)
 {

uri = "<a href="http://localhost:3602/RestService.svc/">http://localhost:3602/RestService.svc/</a>";
 // Calling without parameter
 Console.WriteLine(AddWithoutParameter());
 Console.Read();
 //Calling with parameter
 NumberService obj = new NumberService() { Number1 = 7, Number2 = 2 };
 Console.WriteLine(AddWithParameter(obj));
 Console.Read();

}
 public static string  AddWithoutParameter()
 {
 using (HttpResponseMessage response = new HttpClient().Get(uri))
 {

 int res = response.Content.ReadAsDataContract<int>();
 return res.ToString();
 }
 }
 public static string  AddWithParameter(NumberService obj)
 {
 using (HttpResponseMessage  response = new HttpClient().Post(uri,HttpContentExtensions.CreateDataContract(obj)))
 {

 int res = response.Content.ReadAsDataContract<int>();
 return res.ToString();

}

}

}
 }
}

&nbsp;

 

There are two static methods

AddWithoutParameter() -> To Invoke HTTP Get on Service URI. {In Green}

AddWithParameter() -> To Invoke HTTP POST on Service URI. {In Yellow}

Output

clip_image019

Navigation between pages in Windows 7 Phone

To see how could we do navigation between pages in Windows 7 Phone application , I am going to create a new page . I am giving name of the page as NewPage.Xaml

Now we will navigate as

clip_image001

Let us put a button on MainPage.xaml and on click of that button we will navigate tp NewPage.Xaml

clip_image003

On click event,


void btnToNavigate_Click(object sender, RoutedEventArgs e)
 {

 NavigationService.Navigate(new Uri("/NewPage.xaml", UriKind.Relative));

 }

 

There is one more way to navigate to other page

 


void btnToNavigate_Click(object sender, RoutedEventArgs e)
 {

 PhoneApplicationFrame frame = Application.Current.RootVisual as PhoneApplicationFrame;
 frame.Navigate(new Uri("/NewPage.xaml", UriKind.Relative));
 }

 

 

Now if we want to pass value from one page to another page then,


void btnToNavigate_Click(object sender, RoutedEventArgs e)
 {
 string param1="Value1";
 string param2="Value2";
 NavigationService.Navigate(new Uri(string.Format("/NewPage.xaml?val1={0}&val2={1}",param1 ,param2), UriKind.Relative));

 }

 

In above code, we are passing param1 and param2 to next page.

Now to fetch these values from query parameter on NewPage.Xaml.cs , we need to override the onNavigated() method.

And code would be like

 protected  override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs  e)
 {
 base.OnNavigatedTo(e);
 string value1 = "";
 string value2 = "";
 NavigationContext.QueryString.TryGetValue("val1", out value1);
 NavigationContext.QueryString.TryGetValue("val2", out value2);
 MessageBox.Show(value1);
 MessageBox.Show(value2);
 }