这是我在运行从Asp.net迁移到Windows Azure Cloud的Web项目时遇到的异常。如果我尝试运行ASP.net应用程序它很好,但在迁移到Windows Azure Cloud后,我收到以下异常。我找不到解决方案的任何线索,请一定要做好。
**Exception:**
Server.GetLastError().GetBaseException()
{"Response is not available in this context."}
[System.Web.HttpException]: {"Response is not available in this context."}
Data: {System.Collections.ListDictionaryInternal}
HelpLink: null
InnerException: null
Message: "Response is not available in this context."
Source: "System.Web"
StackTrace: " at System.Web.UI.Page.get_Response()\r\n at Writehome_aspxToXml(XmlWriterDelegator , Object , XmlObjectSerializerWriteContext , ClassDataContract )\r\n at System.Runtime.Serialization.ClassDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter, Object obj, XmlObjectSerializerWriteContext context)\r\n at System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteDataContractValue(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle)\r\n at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeAndVerifyType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, Boolean verifyKnownType, RuntimeTypeHandle declaredTypeHandle, Type declaredType)\r\n at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithXsiType(XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle objectTypeHandle, Type objectType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle, Type declare
dType)\r\n at System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerialize(XmlWriterDelegator xmlWriter, Object obj, Boolean isDeclaredType, Boolean writeXsiType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle)\r\n at System.Runtime.Serialization.XmlObjectSerializerWriteContextComplex.InternalSerialize(XmlWriterDelegator xmlWriter, Object obj, Boolean isDeclaredType, Boolean writeXsiType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle)\r\n at WritePlaceHolderToXml(XmlWriterDelegator , Object , XmlObjectSerializerWriteContext , ClassDataContract )\r\n at System.Runtime.Serialization.ClassDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter, Object obj, XmlObjectSerializerWriteContext context)\r\n at System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteDataContractValue(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle)\r\n at System.Runtime.Serialization.XmlObjectSerializerWriteContext.S
erializeAndVerifyType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, Boolean verifyKnownType, RuntimeTypeHandle declaredTypeHandle, Type declaredType)\r\n at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithXsiType(XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle objectTypeHandle, Type objectType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle, Type declaredType)\r\n at System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerialize(XmlWriterDelegator xmlWriter, Object obj, Boolean isDeclaredType, Boolean writeXsiType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle)\r\n at System.Runtime.Serialization.XmlObjectSerializerWriteContextComplex.InternalSerialize(XmlWriterDelegator xmlWriter, Object obj, Boolean isDeclaredType, Boolean writeXsiType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle)\r\n at WriteArrayOfControlToXml(XmlWriterDelegator , Object , XmlObjectSerializerWriteContext , Collection
DataContract )\r\n at System.Runtime.Serialization.CollectionDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter, Object obj, XmlObjectSerializerWriteContext context)\r\n at System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteDataContractValue(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle)\r\n at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeAndVerifyType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, Boolean verifyKnownType, RuntimeTypeHandle declaredTypeHandle, Type declaredType)\r\n at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithXsiType(XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle objectTypeHandle, Type objectType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle, Type declaredType)\r\n at System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerialize(XmlWriterDelegator xmlWriter, Object obj, Boolean isDec
laredType, Boolean writeXsiType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle)\r\n at System.Runtime.Serialization.XmlObjectSerializerWriteContextComplex.InternalSerialize(XmlWriterDelegator xmlWriter, Object obj, Boolean isDeclaredType, Boolean writeXsiType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle)\r\n at WriteArrayOfanyTypeToXml(XmlWriterDelegator , Object , XmlObjectSerializerWriteContext , CollectionDataContract )\r\n at System.Runtime.Serialization.CollectionDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter, Object obj, XmlObjectSerializerWriteContext context)\r\n at System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteDataContractValue(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle)\r\n at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithoutXsiType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle)\r\n a
t System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerialize(XmlWriterDelegator xmlWriter, Object obj, Boolean isDeclaredType, Boolean writeXsiType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle)\r\n at System.Runtime.Serialization.XmlObjectSerializerWriteContextComplex.InternalSerialize(XmlWriterDelegator xmlWriter, Object obj, Boolean isDeclaredType, Boolean writeXsiType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle)\r\n at WriteSerializableSessionStateStoreDataToXml(XmlWriterDelegator , Object , XmlObjectSerializerWriteContext , ClassDataContract )\r\n at System.Runtime.Serialization.ClassDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter, Object obj, XmlObjectSerializerWriteContext context)\r\n at System.Runtime.Serialization.NetDataContractSerializer.InternalWriteObjectContent(XmlWriterDelegator writer, Object graph, DataContract contract, Hashtable surrogateDataContracts)\r\n at System.Runtime.Serialization.NetDataContractSerializer.InternalWr
iteObject(XmlWriterDelegator writer, Object graph)\r\n at System.Runtime.Serialization.XmlObjectSerializer.InternalWriteObject(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver)\r\n at System.Runtime.Serialization.XmlObjectSerializer.WriteObjectHandleExceptions(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver)\r\n at System.Runtime.Serialization.XmlObjectSerializer.WriteObject(XmlDictionaryWriter writer, Object graph)\r\n at Microsoft.ApplicationServer.Caching.NetDataContractCacheObjectSerializer.Serialize(Stream stream, Object value)\r\n at Microsoft.ApplicationServer.Caching.DataCacheObjectSerializationProvider.SerializeUserObject(Object userObject, Boolean isCompressionEnabled, ValueFlagsVersion flagsType)\r\n at Microsoft.ApplicationServer.Caching.SocketClientProtocol.SerializeValue(Object obj)\r\n at Microsoft.ApplicationServer.Caching.SocketClientProtocol.PutAndUnlock(String key, Object value, DataCacheLockHandle lockHandle,
TimeSpan timeout, DataCacheTag[] tags, String region, IMonitoringListener listener)\r\n at Microsoft.ApplicationServer.Caching.DataCache.InternalPutAndUnlock(String key, Object value, DataCacheLockHandle lockHandle, TimeSpan timeout, DataCacheTag[] tags, String region, IMonitoringListener listener)\r\n at Microsoft.ApplicationServer.Caching.DataCache.<>c__DisplayClass9c.<PutAndUnlock>b__9b()\r\n at Microsoft.ApplicationServer.Caching.MonitoringListenerFactory.EmptyListener.Microsoft.ApplicationServer.Caching.IMonitoringListener.Listen[TResult](Func`1 innerDelegate)\r\n at Microsoft.ApplicationServer.Caching.DataCache.PutAndUnlock(String key, Object value, DataCacheLockHandle lockHandle, TimeSpan timeout)\r\n at Microsoft.Web.DistributedCache.DataCacheWrapper.PutAndUnlock(String key, Object value, DataCacheLockHandle lockHandle, TimeSpan timeout)\r\n at Microsoft.Web.DistributedCache.DataCacheForwarderBase.<>c__DisplayClass1c.<PutAndUnlock>b__1b()\r\n at Microsoft.Web.DistributedCache.DataCacheFo
rwarderBase.<>c__DisplayClass31`1.<PerformCacheOperation>b__30()\r\n at Microsoft.Web.DistributedCache.DataCacheRetryWrapper.PerformCacheOperation(Action action)\r\n at Microsoft.Web.DistributedCache.DataCacheForwarderBase.PerformCacheOperation[TResult](Func`1 func)\r\n at Microsoft.Web.DistributedCache.DataCacheForwarderBase.PutAndUnlock(String key, Object value, DataCacheLockHandle lockHandle, TimeSpan timeout)\r\n at Microsoft.Web.DistributedCache.BlobBasedSessionStoreProvider.SetAndReleaseItemExclusive(HttpContextBase context, String id, SessionStateStoreData item, Object lockId, Boolean newItem)\r\n at Microsoft.Web.DistributedCache.DistributedCacheSessionStateStoreProvider.SetAndReleaseItemExclusive(HttpContext context, String id, SessionStateStoreData item, Object lockId, Boolean newItem)\r\n at System.Web.SessionState.SessionStateModule.OnReleaseState(Object source, EventArgs eventArgs)\r\n at System.Web.HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Exec
ute()\r\n at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)"
TargetSite: {System.Web.HttpResponse get_Response()}
答案 0 :(得分:0)
如果您尝试使用Application_Start
方法访问blob等Azure内容,就会发生这种情况。
这是因为HttpContext.Current.Response
是可访问的.Azure使用Response对象进行日志记录。在任何情况下,如果您需要访问Web应用程序中的blob或执行任何其他Azure内容,并且即使没有Response对象也希望这样做,例如在Application_Start
方法中。您只需设置HttpContext.Current to null
,但请以聪明的方式设置。
Application_Start如下
protected void Application_Start(object sender, EventArgs e)
{
CloudStorageAccount.SetConfigurationSettingPublisher((configName, configSetter) =>
{
configSetter(RoleEnvironment.GetConfigurationSettingValue(configName));
});
using (new AzureContext())
{
CloudStorageAccount storageAccount = CloudStorageAccount.FromConfigurationSetting("BlobConnectionString");
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");
container.CreateIfNotExist();
container.FetchAttributes();
}
}
AzureContext如下:
public class AzureContext : IDisposable
{
HttpContext _oldHttpContext;
bool _restoreOldHttpContext = false;
public AzureContext(bool forceSettingContextToNull = false)
{
if (forceSettingContextToNull)
{
_oldHttpContext = HttpContext.Current;
HttpContext.Current = null;
_restoreOldHttpContext = true;
}
else
{
try
{
HttpResponse response = HttpContext.Current.Response;
}
catch (HttpException)
{
_oldHttpContext = HttpContext.Current;
HttpContext.Current = null;
_restoreOldHttpContext = true;
}
}
}
public void Dispose(bool disposing)
{
if (disposing)
{
if (_restoreOldHttpContext)
{
HttpContext.Current = _oldHttpContext;
}
}
}
public void Dispose()
{
Dispose(true);
}
~AzureContext()
{
Dispose(false);
}
}
有关详细信息,请查看Azure and Response is not available in this context
我希望这会对你有所帮助。
答案 1 :(得分:0)
我因为这个问题而受苦......你可以通过应用这个补丁来解决这个问题......
http://support.microsoft.com/kb/2468871
我已在本地环境以及云环境中使用此修补程序(使用启动角色)。但是您可能不需要现在修补云环境,因为它们可能会更新。如果问题仍然存在于云中,您可以联系Azure支持(印度:1-(866)676-6546),它们将帮助您应用补丁。
答案 2 :(得分:0)
感谢您的帮助,最后我找到了问题的原因。发生问题是因为我试图将System.Web.UI.WebControls存储到会话中。
由于并非所有System.Web.UI.WebControl都可以进行服务化,因此请避免将WebControl存储到会话中。
快乐编码..