fetching and updating data from web service - asp.net

Data can be fetched into an application through web service can it be possible to update data in web service from our application ..

In general a Web Service implementation can be any arbitrary code, so insert/update/delete is nothing special.
I suspect that there's something behind your question, some specific issue in mind? You may want to explain why you think there's a problem.

Of course create a web method that takes parameters and then save the data as required
[WebMethod]
public bool UpdateData(string firstName, string lastName, int id)
{
// do some data access code here
}
Then use in your code in a similar way as to when your are acquiring data from the web service:
MyWebServiceClient client = new MyWebServiceClient();
bool updated = client.UpdateData("Jon", "Skeet", 1);

Related

Create Database If Not Exist Without Restarting Application

I am creating dynamic connection strings in my project. They're created on the fly with the information provided specifically for every user. When the application first fires off, if a database doesn't exist (first time user logs on), a new database is created without problems with this initializer:
public DataContext() : base()
{
// ProxyCreation and LazyLoading doesn't affect the situation so
// comments may be removed
//this.Configuration.LazyLoadingEnabled = false;
//this.Configuration.ProxyCreationEnabled = false;
string conStr = GetDb();
this.Database.Connection.ConnectionString = conStr;
}
The problem is, with this method, I have to restart the application pool on the server and the new user should be the first accessor to the application.
I need the same thing without a requirement of restarting the app. Is that possible?
(This is a SPA using AngularJS on MVC views and WebApi as data provider - May be relevant somehow, so thought I should mention)
I already tried this, but this creates an error for EF and the application doesn't start at all...
You could try a little bit different approach to connect directly (and create) the right database.
public class DataContext : DbContext
{
public DataContext(DbConnection connection) : base(connection, true) { }
}
Here you create the DbContext already with the right connection.
Take also care because you need to specify to migrations that the right connection should be used (not the Web.Config connection but the connection that raised the database creation).
See the second overload here https://msdn.microsoft.com/en-US/library/hh829099(v=vs.113).aspx#M:System.Data.Entity.MigrateDatabaseToLatestVersion.

does onDisconnect event on SignalR2 releases all of its resources upon disconnect?

I have a concurrent dictionary List on my controller that stores list of online users. For example when Client A and Client B connects there are 2 online clients present on the list, but when i disconnect B and then re- connect again it must still show 2 online clients but in my case, only Client B detected as online user(disconnected then reconnected). I am using IIS server 7.5.. Please help me with this, do i need to use a database rather than dictionary? I think it resets the dictionary to zero if one user disconnects and reconnects again.... :( Below is my hub class code
public class Chat : Hub
{
//add online client
private static ConcurrentDictionary<string, string> personLists
= new ConcurrentDictionary<string, string>();
public void Connect(string Username, int ID)
{
string id = Context.ConnectionId;
if (!personLists.ContainsKey(Username))
{
personLists.TryAdd(Username, id);
Clients.Caller.viewOnlinePersons(personLists.Where(p => p.Key != Username));
Clients.Others.enters(Username);
}
else
{
string notif = "user: "+Username+" is already used";
Clients.Caller.onUse(notif);
}
The concurrent dictionary should work just fine. It'd be good if you could post some code, but if you go this route with concurrent dictionary, make sure it's static (the Hub classes get created and destroyed per signal) and I think it'd be better suited placed on the hub itself and private (and of course static, again). You can also use Dependency Injection with SignalR which will be a lot cleaner.
You'll only need database as a backplane if you plan on running the application on multiple servers where of course a memory of a single server is not accessible by the other servers and a duplicate dictionary would be created for each server, so in that case you need to take the storage and move it up a bit in the architecture to be accessible by all the servers.

Asp.net static object appears sometimes as non global

I have an android application as client and ASP.Net + ADO.Net hosted on GoDaddy server. The asp.net has static data structure which stores (among other) the android locations, this data structure is persist also to SQL server 2008 data base. each time the android client change position it send its location to the server which update the data structure and data base. each time the applicaton start the data structure is filled according to the persist data from the data base.
The Problems : altough the data structure is static thus should be global it looks like sometimes there are more then one instances of this static data structure.
Is anyone aware of this issue ?
How can i prevent this from hapening ?
Nathan
This object is has actually a dictionary with name as key and info as
value. This object is updated according to android client which access
the asp.net via HTTP thorugh aspx Page_Load :
HttpContext.Current.Application.Lock() .
The first error is that you use that kind of lock and not the correct one.
The Application.Lock() and Application.Unlock() are designed as lock mechanism only for the Application variables.
To correct lock your static Dictionary you need to use the lock(){} as:
private static readonly object syncLock = new object();
public static Dictionary<int, int> DictMem = new Dictionary<int, int>();
... inside some function....
lock (syncLock)
{
// actions with your dictionary
DictMem[2]=3;
}
Also I like to tell you that the static is not guaranty that you have only one on your program. If the provider use web garden you have more than one, and when the pool recycles then you loose them.

Strange change in parameter types in WCF Workflow Service Application

I'll explain what I've done so far. I'm using VS2010.
Firstly I've created a ASP.NET Web Service Application (framewkork 3.5) with a service with these operations:
[WebMethod]
public Boolean ShoppingTripNeeded(DateTime d)
{
DBConnection db = new DBConnection();
return db.ShoppingTripNeeded(d);
}
[WebMethod]
public String[] ShopsToVisit(DateTime d)
{
DBConnection db = new DBConnection();
return db.ShopsToVisit(d);
}
[WebMethod]
public Item[] ItemsToBuy(DateTime d, String shop)
{
DBConnection db = new DBConnection();
return db.ItemsToBuy(d, shop);
}
And now I'm creating a WCF Workflow Service Application, in which I want to call sequently the 3 methods above, so I've added a Service Reference to my service wsdl here:
http://awtassignment3-shoppinglistservice1.cloudapp.net/Service1.asmx?WSDL
This referencing adds the 3 operations. The first one "ShoppingTripNeeded" seems to be fine (receiving a DateTime and returning a Boolean), but for the other operations, the parameters have changed in a strange way...
For example the operation ShopsToVisit now ask for a ShopsToVisitRequestBody and returns a ShopsToVisitResponseBody... I don't know why this happens! because the first operation is fine...
Moreover, as I'm working with a workflow, I can't "play" with this types to find out what's going on...
Have you any guess? any help will be fine...
Thanks very much!
Is there a reason you are using ASMX? WCF has replaced Web Services as far more superior service communication technology. See SO: Web Services — WCF vs. Standard, SO: Web Service vs WCF Service
To answer your question:
WF 3.5 will wrap any operation with Request/Response message pattern that is not a primitive (e.g. bool, int). String[] is not a primitive type hence it will be wrapped.
WF 4.0 adding service reference (dialog) will wrap all operations in Request/Response message pair by default regardless if this is primitive or complex type. On top of that, it will create Activity for each operation that it discovers.
Using Request/Response message pattern allows for controlling the message shape specifically message headers.
For Reference:
SO: When should I use Message Contracts instead of DataContract and why?
MSDN: Message Contracts

WCF Data Services ODATA

I am working on WCF Data Services. I have a function in my service which adds a record into a table in SQL DB and return the ID of the Inserted record.
I Used to Pass the values to be inserted in the form of Parameters to the Function.
For Example
public int Add(string Name, string Password)
{
// Here I will Add the record and return the ID of the record added in DB
}
But I dont want to do this way of passing in the form of parameters.
I want to pass the object directly.
Like
public int Add(User user)
{
// Here I will Add the record and return the ID of the record added in DB
}
I wrote the function like above in my services and my services project is successfull. When I update my ServiceRefrences I get Error.
It says only primitive types are supported. Is there any work around
this problem.
Thanks For Your Time in Answering my Question.
WCF Data Services (and OData protocol for that matter) only support passing primitive values as parameters to service operations.
The OData way of adding a new entity into an entity set is to send a POST with the entity in the payload to the entity set URL. In the WCF DS Service this will get translated into several calls to the IUpdatable interface on your context.

Resources