Retrieving online data with a wcf service - c#

I'm facing a little problem, and I hope that you will have a solution.
I'm using a wcf service to retrieve online data (from yahoo finance).
This service calls an API which connect to yahoo in order to retrieve the data I need.
However, when I call the API, I get the error
An exception occurred during a WebClient request.
I'm calling this service from console application.
I hope that you can help me with this issue.
== Here is the code
/* wcf service Interface */
[ServiceContract]
public interface IService1
{
[OperationContract]
string GetData(int value);
// TODO: Add your service operations here
}
/* the service calls this function which belongs to another c# project */
public String updateDataBase()
{
DBHandler handler = new DBHandler();
/* Goes online et retrieve the data, when its called, the exception occurs */
handler.updateData();
return "success";
}

I solved my problem by running visual studio as Administrator.
Thank you.

Related

REST Web Service in C# class library

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.

How to maintain Session in WCF Service

I have a WCF Service in which I want to maintain session for my Authentication method.
I have gone through from various articles and applied some of the below changes which are required to maintain session in WCF Service, as WCF not supported Session by default.
1- [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)] in svc file.
2- [ServiceContract(SessionMode = SessionMode.Allowed)] in ServiceContract
3- Use the wsHttpBinding as basicHttpBinding not supported Session.
I am using WCFTestClient to call my service. I have checked the config of my TestClient and it is using basicHttpBinding, here is the cause of issue.
I am unable to implement the 3 point in my Service webconfig and also unable to change the config of my TestClient. Can anyone please guide me. Thanks
To solve this I implemented my own SessionHandler within the service.
a thread safe singleton class containing a Dictionary<Guid, SessionData>
Service Method: Guid RegisterClient(ClientName clientName) { /* add client to session */ }
Service Method: bool UnregisterClient(Guid clientGuid) { /* remove client from session */ }
Service Method: void DoThisOnServer(Guid clientGuid) { /* the service functionality */}
void CheckTimeout() { /* iterate over dictionary and remove out timed sessions */ }
Hints:
SessionData contains ClientName, TimeOfConnection, YourUsefulData
ClientName is a placeholder for IP-Adresse or some other initial identificator
Client has to register and all following operations are done only if the provided Guid exists in SessionHandler.

Error in access to Entity Framework data from WCF service in web and win form application?

I am in the middle of big web application, I use Entity Framework as my data service, now we need some windows application to work with our data, so I want to give them a service with WCF
But when my client wants to get service some error is happened from my public property which I use for caching Entity Model
public partial class DepositEntities : ObjectContext
{
public static DepositEntities Current
{
get
{
DepositEntities oc =
HttpContext.Current.Items["ObjectContext"] as DepositEntities;
if (oc == null)
{
oc = new DepositEntities();
HttpContext.Current.Items["ObjectContext"] = oc;
}
return oc;
}
}
}
I know the problem is from this line, after I debug my code
DepositEntities oc = System.Web.HttpContext.Current.Items["ObjectContext"] as DepositEntities;
When I change my Current property body to some thing like this
public static DepositEntities Current
{
get
{
DepositEntities oc = new DepositEntities();
return oc;
}
}
everything is OK when I get data from services I have no problem
But everywhere I have join in my codes I have problem because It thinks there are different data source because of new DepositEntities();
You're most likely experiencing problems because WCF doesn't have HttpContext.Current. Read more about contexts in WCF - this question may be a good start: http://social.msdn.microsoft.com/Forums/en/wcf/thread/27896125-b61e-42bd-a1b0-e6da5c23e6fc.
I also think it would be better for you to manage lifetime of an ObjectContext with a DI Container (ie. Castle Windsor). Thanks to this, it won't be necessary to expose static property Current which is a problem for WCF service, unit tests, etc.
Check out "Hosting WCF Services in ASP.NET Compatibility Mode" in wcf service and ASP.NET. It explains how to get a valid HttpContext in a wcf service.

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

Magento Web Service Errors

I am implementing a custom solution to interface with a Magento website. My code is in C#. I am trying to create products using either the v2_soap API and the xml-rpc API web services. I have attempted to create a product using both services. I cannot seem to successfully create a product. With each service I receive the error message [102] Invalid data given. Details in error message.. I have tried passing in a variety of data to the api call, but have not had any luck. I am wondering a few things:
1) Is there any way to recieve better error messages about what data is not valid when I make a web service call? The error message seems to indicate that I can get details somewhere, but I have searched through all logs, error message data I can find with no luck.
2) What are the minimum attributes required to add a new product using the web service?
Here is a bit of the code I am using. This is the XML-RPC implementation. I am using the cook computing xml-rpc library.
public int CreateProduct(Product product) {
var entity = ConvertProduct(product);
//int productId = Service.catalogProductCreate(SessionId, "simple", "0", product.Sku, entity);
int productId = XmlRpcService.CallReturnInt(SessionId, "catalog_product.create",
new object[] {
"simple" /* product type */,
0 /* attribute set */,
product.Sku /* sku */,
entity /* product data */
});
return productId;
}
private XmlRpcStruct ConvertProduct(Product product) {
var entity = new XmlRpcStruct();
entity.Add("name", product.Name);
entity.Add("description", product.Description);
return entity;
}
protected IMagentoXmlRcpService XmlRpcService {
get {
return this.xmlRpcService;
}
}
The key was the attribute set. The default attribute set is 4 (at least for me). That little guy is the root of a lot of problems. The error responses on the Magento web services could really use some work.
See this forum thread for more info: http://www.magentocommerce.com/boards/viewthread/36892/

Categories

Resources