I will consume the Customer WCF Service which I developed in Part 1 on WS* Http Binding, you can implement the best security, transactions and many more things using this way. However, I will consume in a very simplistic way in this post. I assume that you have already deployed the WCF Service under IIS and have a working URL ready to be referenced and you are planning to use the customer WCF service in ASPX web Page.
Create a Test Web Site Project using Visual Studio 2008/5 and focus on following 3 files
1. Web.Config2. CustomerWcfServiceTest.aspx – The page you added for testing the WCF Service3. CustomerWcfServiceTest.aspmx.vb – Code behind file for WCF Service Test Page
Steps: Right Click on the project and click add the Customer WCF Service using Add Service Reference. Let’s say you give it a name called CustomerWcfService.
Web.Config
After adding the above service, verify the web.config file that binding and endpoint information is added to it. This information gets automatically added or removed when you add or remove a service reference. The address will vary as per your machine. You should be able to see like
<system.serviceModel><bindings><wsHttpBinding><bindingname="WSHttpBinding_ICustomerService"closeTimeout="00:01:00"openTimeout="00:01:00"receiveTimeout="00:10:00"sendTimeout="00:01:00"bypassProxyOnLocal="false"transactionFlow="false"hostNameComparisonMode="StrongWildcard"maxBufferPoolSize="524288"maxReceivedMessageSize="65536"messageEncoding="Text"textEncoding="utf-8"useDefaultWebProxy="true"allowCookies="false"><readerQuotasmaxDepth="32"maxStringContentLength="8192"maxArrayLength="16384"maxBytesPerRead="4096"maxNameTableCharCount="16384" /><reliableSessionordered="true"inactivityTimeout="00:10:00"enabled="false" />
<securitymode="Message">
<transportclientCredentialType="Windows"proxyCredentialType="None"realm="" />
<messageclientCredentialType="Windows"negotiateServiceCredential="true"algorithmSuite="Default"establishSecurityContext="true" /></security></binding></wsHttpBinding></bindings><client><endpointaddress="http://vishwa/ExampleService/CustomerService.svc"
binding="wsHttpBinding"bindingConfiguration="WSHttpBinding_ICustomerService"contract="CustomerWcfService.ICustomerService"name="WSHttpBinding_ICustomerService"><identity><dnsvalue="localhost" />
</identity></endpoint></client></system.serviceModel>Remember, you will be using binding Name WSHttpBinding_ICustomerService for consuming this service in code behind file.
CustomerWCFServiceTest.aspx: Add the following code inside the body of this page
<form id="form1" runat="server"><table><tr><td colspan="2"><b>Through Server Side Service Reference</b></td>
</tr><tr><td >Customer ID :</td><td><asp:TextBox ID="txtCustID" runat="server" Text="12345"></asp:TextBox></td></tr><tr><td>Customer Name :</td><td><asp:TextBox ID="txtCustName" runat="server" Text="Joe Smith"></asp:TextBox></td></tr><tr><td>Customer DOB (yyyy-mm-dd):</td><td><asp:TextBox ID="txtCustDOB" runat="server" Text="1988-08-08"></asp:TextBox></td></tr><tr><td>Customer Address:</td><td><asp:TextBox ID="txtCustAddress" runat="server" Text="unknown"></asp:TextBox></td></tr><tr><td><asp:Button ID="btnGetCustomer" runat="server" Text="Get Customer" /></td><td><asp:Button ID="btnAddCustomer" runat="server" Text="Add Customer" /></td></tr><tr><td><asp:Button ID="btnUpdateCustomer" runat="server" Text="Update Customer" /></td><td><asp:Button ID="btnDeleteCustomer" runat="server" Text="Delete Customer" /></td></tr><tr><td><asp:Button id="btnGetCustomers" runat="server" Text="Get All Customers"/></td><td><asp:Label ID="lblStatus" runat="server" Text="Status"ForeColor="#CC3300" style="font-weight: 700"></asp:Label></td></tr></table></form><br /><form id="frmCustomerS" method="post" action="CustomerWcfServiceTest.aspx"><div><table><tr><td colspan="2"><b>Through Client Side SOAP/XML HTTP POST</b></td></tr>
<tr><td>Customer ID :</td><td><input name="ID" id="ID" type="text" value="12345" /> </td></tr><tr><td>Customer Name :</td><td><input name="Name" id="Name" type="text" value="Chris Clark" /> </td></tr><tr><td>Customer DOB (yyyy-mm-dd):</td><td><input name="DOB" id="DOB" type="text" value="1988-08-08"/> </td></tr><tr><td>Customer Address:</td><td><input name="Address" id="Address" type="text" value="unknown"/> </td></tr><tr><td><input type="button" name="btnGetCustomer" value="Get Customer" onclick="GetCustomerSOAP()" /></td><td><input type="button" name="btnAddCustomer" value="Add Customer" onclick="AddCustomerSOAP()" /></td></tr><tr><td><input type="button" name="btnUpdateCustomer" value="Update Customer" onclick="UpdateCustomerSOAP()" /></td><td><input type="button" name="btnDeleteCustomer" value="Delete Customer" onclick="DeleteCustomerSOAP()"/></td></tr><tr><td colspan="2"><input type="button" name="btnGetAllCustomers" value="Get All Customers" onclick="GetCustomersSOAP()"/></td></tr></table></div></form>
CustomerWCFServiceTest.aspx.vb: Add the following code to handle the events
Partial Class CustomerServiceTest
Inherits System.Web.UI.Page
Protected Sub btnGetCustomer_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGetCustomer.ClickDim wcfSrv As New CustomerWcfService.CustomerServiceClient("WSHttpBinding_ICustomerService")Dim cust As CustomerWcfService.Customer = wcfSrv.GetCustomer(CInt(Me.txtCustID.Text))If cust.ID > 0 ThenMe.txtCustID.Text = cust.ID
Me.txtCustName.Text = cust.Name
Me.txtCustDOB.Text = cust.DOB
Me.txtCustAddress.Text = cust.Address
Me.lblStatus.Text = "Customer found."Else
Me.lblStatus.Text = "Customer not found."Me.txtCustName.Text = ""Me.txtCustDOB.Text = ""End IfwcfSrv.Close()End SubProtected Sub btnAddCustomer_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddCustomer.ClickDim wcfSrv As New CustomerWcfService.CustomerServiceClient("WSHttpBinding_ICustomerService")Dim cust As New CustomerWcfService.Customercust.Name = Me.txtCustName.Text
cust.DOB = CDate(Me.txtCustDOB.Text)cust.Address = Me.txtCustAddress.Text
Dim rtn As Integer = wcfSrv.AddCustomer(cust)lblStatus.Text = "Cust ID: " & rtn.ToString
wcfSrv.Close()End SubProtected Sub btnUpdateCustomer_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpdateCustomer.ClickDim wcfSrv As New CustomerWcfService.CustomerServiceClient("WSHttpBinding_ICustomerService")Dim cust As New CustomerWcfService.Customercust.ID = CInt(Me.txtCustID.Text)cust.Name = Me.txtCustName.Text
cust.DOB = CDate(Me.txtCustDOB.Text)cust.Address = Me.txtCustAddress.Text
Dim rtn As Boolean = wcfSrv.UpdateCustomer(cust)lblStatus.Text = "Update Status is : " & rtn.ToString
wcfSrv.Close()End SubProtected Sub btnDeleteCustomer_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDeleteCustomer.ClickDim wcfSrv As New CustomerWcfService.CustomerServiceClient("WSHttpBinding_ICustomerService")Dim rtn As Boolean = wcfSrv.DeleteCustomer(CInt(Me.txtCustID.Text))lblStatus.Text = "Delete Status is : " & rtn.ToString
wcfSrv.Close()End SubProtected Sub btnGetCustomers_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGetCustomers.ClickDim wcfSrv As New CustomerWcfService.CustomerServiceClient("WSHttpBinding_ICustomerService")Dim custArr() As CustomerWcfService.CustomercustArr = wcfSrv.GetCustomers()For Each cust As CustomerWcfService.Customer In custArrResponse.Write("ID: " & cust.ID.ToString & " Name: " & cust.Name & "<br/>")Next
lblStatus.Text = "No of Customer Records found: " & custArr.Length.ToString
wcfSrv.Close()End SubEnd ClassYou are done. Run the project and test the page, you should be able to Add, Modify, Delete and Get Customer records.
No comments:
Post a Comment