Question

I'm new to WCF ,I Developed a simple WCF service Application in which i added entityframework data model. I have a method which return one field of a table from database. I'm working on visual studio 2013 and sql server 2012 on windows 8. When i run it in IISExpress everything is ok.But when i run it from iis ,it display the below error:

Request Error
The server encountered an error processing the request. The exception message is
 'The  underlying provider failed on Open.'. See server logs for more details. The exception stack trace is:

at System.Data.Entity.Core.EntityClient.EntityConnection.Open() at
System.Data.Entity.Core.Objects.ObjectContext.EnsureConnection() at   
System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, 
IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean  
releaseConnectionOnSuccess) at System.Data.Entity.Core.Objects.ObjectQuery`1.
<>c__DisplayClassb.<GetResults>b__9() at 
System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 
operation) at System.Data.Entity.Core.Objects.ObjectQuery`1.GetResults(Nullable`1 
forMergeOption) at System.Data.Entity.Core.Objects.ObjectQuery`1.
<System.Collections.Generic.IEnumerable<T>.GetEnumerator>b__0() at 
System.Lazy`1.CreateValue() at System.Lazy`1.LazyInitValue() at 
System.Data.Entity.Internal.LazyEnumerator`1.MoveNext() at 
WcfRestService.Service1.SelectRandomQuestion() at SyncInvokeSelectRandomQuestion(Object 
, Object[] , Object[] ) at 
System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] 
inputs, Object[]& outputs) at 
 System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc) 
 at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& 
rpc) at 
System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc& 
rpc) at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean 
isOperationContextSet)

This is My Service1.svc.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Data.Services.Providers;
using System.Text;

namespace WcfRestService
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class     name "Service1" in code, svc and config file together.
// NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
[System.ServiceModel.ServiceBehavior(IncludeExceptionDetailInFaults = true)] 

public class Service1 : EntityFrameworkDataService<CollaborationDBEntities>, IService1
{

    public string SelectRandomQuestion()
    {
        using (CollaborationDBEntities db = new CollaborationDBEntities())
        {
            var q = from questions in db.QuestionTable
                    select questions.QText;
            List<string> list = new List<string>();
            foreach (var item in q)
            {
                list.Add(item);
            }
            Random random = new Random();
            int selectedIndex = random.Next(0, list.Count);

            return list[selectedIndex];
        }
    }
    public string AddNewUser(string userName, string fName, string lName, string email, string imei)
    {
        using (CollaborationDBEntities db = new CollaborationDBEntities())
        {
            UserTable userObject = new UserTable() { UserName = userName, UserFName = fName, UserLName = lName, UserNumber = imei };
            db.UserTable.Add(userObject);
        }

        return null;
    }

I Searched alot and did what they say here but noting changed. any help really appreciate. best regards.

Was it helpful?

Solution

Most likely it's permission issue.

1) if you connecting to local DB, here is answer MSSQL Error 'The underlying provider failed on Open'

2) if you connecting to remote server, check firewall settings on DB server

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top