{"id":876,"date":"2010-04-15T11:28:41","date_gmt":"2010-04-15T10:28:41","guid":{"rendered":"http:\/\/www.codingfriends.com\/?p=876"},"modified":"2010-04-15T11:34:09","modified_gmt":"2010-04-15T10:34:09","slug":"web-service","status":"publish","type":"post","link":"https:\/\/www.codingfriends.com\/index.php\/2010\/04\/15\/web-service\/","title":{"rendered":"Web service"},"content":{"rendered":"<p>An web service is basically like a <a href=\"http:\/\/www.codingfriends.com\/index.php\/2010\/04\/12\/soap-server\/\">SOAP server<\/a> in that it &#8220;talks&#8221; from the client-server in XML with the client requesting the function on the server and obtaining a result.<\/p>\n<p>I am using <a href=\"http:\/\/www.mono-project.com\/Main_Page\">mono<\/a> to compile and run the web service since I am running apache on Linux (kubuntu) (<a href=\"http:\/\/www.codingfriends.com\/index.php\/2010\/04\/14\/mono-web-development-on-linux\/\">here<\/a> is how I setup the mono apache module within k\/ubuntu).<\/p>\n<p>To start with, you need to create a web service instance and what language you are going to be using (since using mono then c#) and also the class that you want to &#8220;expose&#8221; to the web service itself.<\/p>\n<pre lang=\"csharp\">\r\n<%@ WebService language=\"C#\" class=\"FirstWebService\" %>\r\n<\/pre>\n<p>after that you then need to add the attribute to the class to tell the compiler that the class is going to be a webservice and what the base directory is going to be (I created a new directory within the apache hosting directory), since we are writing a WebService we need to inherit from a System.Web.Services.WebService <\/p>\n<pre lang=\"csharp\">\r\n[WebService(Namespace=\"http:\/\/localhost\/csharp\/\")]\r\npublic class FirstWebService : WebService\r\n<\/pre>\n<p>and then just within the class structure you only need to tell the function with a attribute heading of [WebMethod] that it is going to be &#8220;exposed&#8221; to the web service, if you do not put that in, it will be &#8220;exposed&#8221; to the web service and thus the client cannot access that method.<\/p>\n<pre lang=\"csharp\">\r\n    [WebMethod]\r\n    public int Add(int a, int b)\r\n<\/pre>\n<p>and that is about it, the mono (and of course .net base framework) will create the rest of the WSDL and additional parts for a WebService.<\/p>\n<p>Here is the full web service code in full, save this as web_service.asmx (the asmx means a web service extension)<\/p>\n<pre lang=\"csharp\">\r\n<%@ WebService language=\"C#\" class=\"FirstWebService\" %>\r\n\r\nusing System;\r\nusing System.Web.Services;\r\n\r\n\/\/ expose as the web service\r\n[WebService(Namespace=\"http:\/\/localhost\/csharp\/\")]\r\npublic class FirstWebService : WebService\r\n{\r\n    \/\/ expose as a web method\r\n    [WebMethod]\r\n    public int Add(int a, int b)\r\n    {\r\n        return TheAddingMethod(a,b);\r\n    }\r\n\r\n    \/\/ this one will not be exposed since it does not have the [WebMethod] attribute\r\n    public int TheAddingMethod(int a, int b)\r\n    {\r\n\t\/\/ but since it is part of the class you can still call class methods etc.\r\n\treturn a+b;\r\n    }\r\n}<\/pre>\n<p>and when you goto the web URL for the webservice you should see something similar to this<\/p>\n<figure id=\"attachment_877\" aria-describedby=\"caption-attachment-877\" style=\"width: 398px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.codingfriends.com\/wp-content\/uploads\/2010\/04\/firstwebservice.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.codingfriends.com\/wp-content\/uploads\/2010\/04\/firstwebservice.png\" alt=\"The FirstWebService URL\" title=\"FirstWebService\" width=\"398\" height=\"254\" class=\"size-full wp-image-877\" srcset=\"https:\/\/www.codingfriends.com\/wp-content\/uploads\/2010\/04\/firstwebservice.png 398w, https:\/\/www.codingfriends.com\/wp-content\/uploads\/2010\/04\/firstwebservice-300x191.png 300w\" sizes=\"auto, (max-width: 398px) 100vw, 398px\" \/><\/a><figcaption id=\"caption-attachment-877\" class=\"wp-caption-text\">The FirstWebService URL<\/figcaption><\/figure>\n<p>if you click on the left menu &#8220;add&#8221; and then &#8220;test form&#8221; to test the webservice, it will bring up a window similar to the below, I have done a full test with adding 4 + 5 = 9<\/p>\n<figure id=\"attachment_880\" aria-describedby=\"caption-attachment-880\" style=\"width: 723px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.codingfriends.com\/wp-content\/uploads\/2010\/04\/firstwebservice2.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.codingfriends.com\/wp-content\/uploads\/2010\/04\/firstwebservice2.png\" alt=\"Testing the first web service\" title=\"FirstWebService test\" width=\"723\" height=\"403\" class=\"size-full wp-image-880\" srcset=\"https:\/\/www.codingfriends.com\/wp-content\/uploads\/2010\/04\/firstwebservice2.png 723w, https:\/\/www.codingfriends.com\/wp-content\/uploads\/2010\/04\/firstwebservice2-300x167.png 300w\" sizes=\"auto, (max-width: 723px) 100vw, 723px\" \/><\/a><figcaption id=\"caption-attachment-880\" class=\"wp-caption-text\">Testing the first web service<\/figcaption><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>An web service is basically like a SOAP server in that it &#8220;talks&#8221; from the client-server in XML with the client requesting the function on the server and obtaining a result. I am using mono to compile and run the web service since I am running apache on Linux (kubuntu) (here is how I setup &hellip; <a href=\"https:\/\/www.codingfriends.com\/index.php\/2010\/04\/15\/web-service\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Web service<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[179,180,181],"class_list":["post-876","post","type-post","status-publish","format-standard","hentry","category-c_sharp","tag-web-service","tag-webmethod","tag-webservice"],"_links":{"self":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/876","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/comments?post=876"}],"version-history":[{"count":5,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/876\/revisions"}],"predecessor-version":[{"id":883,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/876\/revisions\/883"}],"wp:attachment":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/media?parent=876"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/categories?post=876"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/tags?post=876"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}