IIS 8中的WCF和EntityFramework错误服务器在处理请求时遇到错误

时间:2014-05-03 05:51:42

标签: c# asp.net wcf entity-framework iis

我是WCF的新手,我开发了一个简单的WCF服务应用程序,其中我添加了entityframework数据模型。 我有一个方法从数据库返回一个表的一个字段。 我正在Windows 8上使用visual studio 2013和sql server 2012。 当我在IISExpress中运行它时一切正常。但是当我从iis运行它时,它显示以下错误:

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)

这是我的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;
    }

我搜索了很多并做了他们所说的here,但注意到了改变。 任何帮助真的很感激。 最好的问候。

1 个答案:

答案 0 :(得分:0)

很可能是它的许可问题。

1)如果您连接到本地数据库,请回答MSSQL Error 'The underlying provider failed on Open'

2)如果连接到远程服务器,请检查数据库服务器上的防火墙设置