REST Web Service in C# class library - c#

I'm trying to create a REST Webservice, not sure to create a separate WebAPI or just add a WCF Service.
public class FirstService : Reactor, IFirstService
{
const string StateFilename = "DetectedEvent.bin";
public string Reset()
{
string stateFile = Path.Combine(App.StoragePath, StateFilename);
if (File.Exists(stateFile))
{
File.Delete(stateFile);
}
return "Reset Completed";
}
}
I am trying to write a webservice for deleting a file and that call I would be doing using Powershell Invoke-RestMethod
The DetectedEvent.bin is located in remote server.
I have already created a C# class library i.e. custom Seq.App.FirstOfType but now I want to create a REST webservice by which wen called can delete a file.
I am using Seq App, Seq has a storagePath which it stores all data, which I want to delete it.
Please someone can let me know how should I go forward i.e should I create new project i.e. WEBAPI and attach it to current solution or I should create a WCF service application.

Related

How I can use a PHP code in C# desktop application

Is there any way to run the PHP code on the c# app
since I'm working on an application that requires to implement of this site's API
but there is no documentation except for PHP, so is there any way I can use this piece of code in a c# application or convert this code in C# if that is possible.
public $api_url = 'https://smm-bomb.com/api/v2'; // API URL
public $api_key = ''; // Your API key
public function order($data) { // add order
$post = array_merge(array('key' => $this->api_key, 'action' => 'add'), $data);
return json_decode($this->connect($post));
}
One option is to rewrite the call using C#, check docs if needed.
Another one is to made your custom php-api which wrap initial call.
Then from C# just use your php-api. Postman can be a useful tool to check your custom php-api and also for C# code generation
---custom php-api---
--update to fit https://smm-bomb.com/api--
<?php
class Api
{
public $api_url = 'https://smm-bomb.com/api/v2'; // API URL
public $api_key = '4a99333333333333333dummy'; // Your API key
...
//just copy-paste from example
}
//from-docs, create an instance an invoke any methods
//since it's a self contained class no imports required
//main
$api = new Api();
$services = $api->services(); # return all services
$out = [];
//mainly here will be the return of api-call
$out["mykey"]="myvalue";
//output as json whatever wanted
$out["listServices"]=$services;
echo json_encode($out);
?>
//sample output
//{"mykey":"myvalue"}
This piece of code should be accessible via link eg: https://yoursite/custom-api (for security purpose can use a token on query-string. /custom-api?key=123mykey ->trivial approach)
Use PostMan to check and see code-generation

forcing to ignore some url parameters

I am making an iPhone app where I am using .NET webservice.
Let's say below is the URL I have.
http://www.myweb.com/wser.asmx/listOfStudents?class=12
Here I was getting list of students with below fields in it.
Name
Roll Number
Class
Now client asked to make arabic version. So we update the query to below.
http://www.myweb.com/wser.asmx/listOfStudents?class=12&appLang=ar
^^^^^^^^^^^^
For testing we update webservice on another server & checked and all is working fine.
Now while uploading app on App store, I noticed if I update actual webservice current app won't work as appLang variable is missing in current app that is there on app store.
If I don't update webservice & apple go in testing, the app will crash as it will throw error of missing parameter appLang.
So what I was thinking is,
I will upload new webservice, BUT appLang will be arabic BY-DEFAULT.
Like if I execute url http://www.myweb.com/wser.asmx/listOfStudents?class=12 with updated webservice (appLang added in webservice but not in url), it will not throw error of parameter missing appLang?
Is there any way to make default parameter?
while using GET isn't that pretty in this case (POST could be more suitable), you could do this :
//by specifying a messageName, you can do overloading with webmethods
[WebMethod (MessageName = "listOfStudentsDefault")]
[ScriptMethod(UseHttpGet=true)]
public string listOfStudents(int class, string appLang)
{
// code here...
}
[WebMethod (MessageName = "listOfStudents")]
[ScriptMethod(UseHttpGet=true)]
public string listOfStudents(int class)
{
return listOfStudents(class, "ar");
}

Web services and client DLL

I have a web service and a client DLL. The web service uses an Oracle database.
For testing the client DLL, I copied the web service and made it point to a test database. Then I copied the client DLL and added this test web service using "Add web reference".
What I would like to do is to use one web service and one client DLL but be able to tell the client DLL to use either use the test or production database rather than two identical web serivces and client DLLs.
Edit
I mis-stated the issue. What I need to do is use one client DLL and two web services (one production version, one development/test version) and be able to, somehow, tell the client DLL which web services to use.
This is a sample of how the web service, client DLL and client app are used:
public class DSSService : System.Web.Services.WebService
{
public DSSService()
{
}
[WebMethod(MessageName = "GetFacility", BufferResponse=true, Description = "blah.")]
public Facility GetFacility(string sFDBID, string sZip, string sFinNo)
{
Facility oFacility = ...;
...
return oFacility;
}
....
}
Client DLL:
namespace DSSConfig
{
string sWSURL;
public class Config
{
public Config()
{
}
public void SetWSURL(string sURL)
{
sWSURL = sURL;
}
public Facility GetFacility(string sFDBID, string sZip, string sFinNo)
{
DSSService Proxy = new DSSService();
proxy.Url = sWSURL;
Facility oFacility = Proxy.GetFacility(sFDBID, sZip, sFinNo);
return oFacility;
}
In client application, having DSSConfig DLL as reference:
DSSConfig oConfig = new DSSConfig();
oConfig.SetWSURL("http://myserver/WebService1/service.asmx");
oConfig.GetFacility("blah", "blah", "blah");
What you need to do is change the WEB Service to take a parameter that it will use to construct the connection string to the DB.
Then change client DLL to pass that parameter as part of the call or connection.
Then you can configure the Client DLL to using any technique you like to pass the parameter. My suggestion is perhaps derive a class from the generated proxy in the client DLL and use this in the client code.
Without specific implementation details I can't be more precise.

Do I need to change web service path everytime when I call it?

I am developing on Windows application in c# and I am using web server's web service in this Windows application.
The web service should be dynamic and I need to change it in the application.
I managed to do it by this code :
CallWebService.MyWS ws = new CallWebService.MyWS();
ws.Url = "new url";
This new url will be set as per client's web server url.
I am calling this web service (I mean web service functions) 20 to 25 times in my application.
Do I need to change this path everytime when I call it or for the first time will be ok ?
Use a fixed port number for your service and configure this url in your app/web.config file and use it in your code.
Create a helper class and use that. Make it configurable by using an app setting or better store in config table in database if you are using one.
If you are using WCF client, you can pass URL in client constructor. Otherwise create a partial class for your webservice to create that constructor.
public class MyWebServiceHelper
{
private string _url = null;
public MyWebServiceHelper()
{
this._url = GetWsUrlFromDbOrAppConfig();
}
public CallWebService.MyWS GetMyWebServiceProxy()
{
return new CallWebService.MyWS("WcfBindingConfig", _url);
}
}

I can't seem to call my web service from C# Forms app

I have a web site which has a simple web service. I can call the web service successfully from javascript on the page. I need to be able to call the same web service from a C# forms application.
The web service code is very simple:
[WebService(Namespace = "http://myurl.com/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class IDCService : System.Web.Services.WebService {
public IDCService () {
}
[WebMethod]
public string HelloWorld() {
return "Hello World";
}
My javascript works:
function HelloWorld() {
var yourName = $get('txtYourName').value;
alert(yourName);
IDCService.HelloWorld(HelloWorldCalback, failureCB);
}
function HelloWorldCalback(result) {
alert(result);
}
function failureCB(result) {
alert("Failed");
}
However, when I try to set a reference to the WS in my C# code what I expect to see is an object with a method "HelloWorld", what I in fact see is an object with properties like "HelloWorldRequest", "HelloWorldResponse", "HelloWorldRequestBody" and so forth.
I am new to web services, and am very confused. Any help would be appreciated.
Depends on how you added your reference :-)
If you added it by clicking "Add Web Reference", you specified the location of the service, and you gave it a namespace - let's assume it would be called "MySVC".
In that case, you should be able to do this in your Winforms program:
MySVC.MyTestService svc = new MySVC.MyTestService();
string message = svc.HelloWorld();
and thus retrieve the output of the HelloWorld method.
On the other hand, if you clicked on "Add Service Reference" (which is not the same - this will add a WCF client side proxy to your web service), then you'd get those request and response object classes. You should also get a xxxxClient class, and that's what you'll use:
MyWCFService.MyTestServiceSoapClient client =
new MyWCFService.MyTestServiceSoapClient();
string message = client.HelloWorld()
That way, you should be able to access all your methods on your web service, too.
Marc

Categories

Resources