2012-06-19 4 views
3

WCF 서비스에서 asp.net 세션에 액세스하려고합니다. 내wcf 서비스 내 asp.net 응용 프로그램에서 호출하고 있습니다. 나는 많은 SO 질문 및 기사를 통과하고 그들이 말한 모든 것을 시도했지만 여전히 나는 wcf 서비스에서 클라이언트 세션에 액세스 할 수 없다. DataSet ds = HttpContext.Current.Session["data"] as DataSet;이라고 쓰면 ds은 항상 null입니다.wcf 서비스에서 asp.net 세션에 액세스 할 수 없습니다.

무엇이 여기에 있습니까? 내가 Global.asaxsession_start의 데이터 세트를 생성하고 세션에 넣어하고 // 인터페이스

[ServiceContract(SessionMode = SessionMode.Allowed)] 
public interface IMyService 
{ 
    [OperationContract(IsInitiating=true)] 
    [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)] 
    string GetData(); 

    [OperationContract] 
    [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)] 
    bool SaveData(string data); 
} 

// 서비스

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)] 
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)] 
public class MyService : IMyService 
{ 
    public string GetData() 
    { 
     return "something"; 

    } 
    public bool SaveData(string data) 
    { 
     DataSet ds = HttpContext.Current.Session["data"] as DataSet; 
     return true; 
    } 

} 

:

이 내 WCF 서비스가 같은 모습입니다 그래서 나는 사용자 세션이 유효한 한 응용 프로그램에서 사용할 수 있습니다.

<service name="MyService"> 
    <endpoint address="" behaviorConfiguration="JSONPBehaviorConfiguration" 
     binding="customBinding" bindingConfiguration="jsonpBinding" 
     contract="IMyService"> 
    </endpoint> 
    </service> 

<customBinding> 
    <binding name="jsonpBinding" > 
     <jsonpMessageEncoding /> 
     <httpTransport manualAddressing="true"/> 
    </binding> 
    </customBinding> 


    <system.serviceModel> 
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/> 
    </system.serviceModel> 

답변

5

은 당신이 ASP.NET 호환성을 활성화해야 ASP.NET이 기능을 사용하려면

protected void Session_Start(object sender, EventArgs e) 
    { 
     DataSet ds = new DataSet(); 

     //Table to hold Product Selection 
     DataTable dt = new DataTable("T1"); 
     dtSSNCertification.Columns.Add("Col1"); 
     ds.Tables.Add(dt); 

     Session.Add("data", ds); 
    } 

이 내 WCF 프로젝트의 web.config bindings이 같은 모습입니다 WCF. web.config에서 다음을 설정하십시오.

<system.serviceModel> 
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"> 
+0

예. 저기에있어. – Asdfg

+1

나는 내 서비스에 AJAX 호출을하고 있기 때문에 작동하지 않습니까? – Asdfg

+0

남자가 작동하지 않았다! –

관련 문제