Reputation: 113
I set up a WCF Service like this:
IService1.cs
namespace AzureWebServiceTest
{
[ServiceContract]
public interface IService1
{
[OperationContract]
void createCloudAccount(string accountName);
[OperationContract]
SqlConnection useDb();
}
}
Service1.cs
namespace AzureWebServiceTest
{
public class Service1 : IService1
{
public void createCloudAccount(string accountName)
{
CloudStorageAccount storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;BlobEndpoint=http://127.0.0.1:10000/devstoreaccount1;TableEndpoint=http://127.0.0.1:10002/devstoreaccount1;QueueEndpoint=http://127.0.0.1:10001/devstoreaccount1");
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference(accountName);
container.CreateIfNotExists();
}
public SqlConnection useDb()
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Server=localhost;Database=StilistaLibrary;Trusted_Connection=true";
return conn;
}
}
}
But i have a problem with the return type of useDB(), i think because SqlConnection is a no-serializable object, how can i set up correctly this service?
Upvotes: 1
Views: 436
Reputation: 151594
It looks like you misunderstand what you would introduce a web service for.
You certainly don't return database connection objects from the service to the client. You use a service to encapsulate and abstract the database from the client instead.
You add methods like GetRecordsForCustomer(int customerID)
to the service, and let the client call that. You don't return database objects, but data contracts from the service.
This way you can not only change the underlying data store (i.e. swap databases, or more common, alter your data model without impacting your clients), but you can also add additional behavior without having to program triggers and procedures on the database.
If you instead want your clients to access the database directly, you have no use for a web service.
Upvotes: 2