whoops - I should have posted this to CSharp not ASPNET:
Hi,
I am using a 3rd party product to create dynamic web service proxies
(downloaded from www.thinktecture.com).
My problem is that it doesn't work if Anonymous Access is turned off. I
know, from searching, that this is to do with Credentials but I just
can't seem to find where to fix it.
First it builds wsdl string (I added the 2 Credentials lines):
WebRequest req = WebRequest.Crea te(uri);
req.Credentials = System.Net.Cred entialCache.Def aultCredentials ;
WebResponse result = req.GetResponse ();
Stream ReceiveStream = result.GetRespo nseStream();
Encoding encode = Encoding.GetEnc oding("utf-8");
StreamReader sr = new StreamReader(Re ceiveStream, encode);
string wsdlSourceValue = sr.ReadToEnd();
sr.Close();
return wsdlSourceValue ;
Then it builds an assembly:
StringReader wsdlStringReade r = new StringReader(st rWsdl);
XmlTextReader tr = new XmlTextReader(w sdlStringReader );
ServiceDescript ion.Read(tr);
tr.Close();
// WSDL service description importer
CodeNamespace cns = new CodeNamespace(C odeConstants.CO DENAMESPACE);
sdi = new ServiceDescript ionImporter();
// check for optional imports in the root WSDL
DiscoveryClient Protocol dcp = new DiscoveryClient Protocol();
//(I added this credentials line)
dcp.Credentials = System.Net.Cred entialCache.Def aultCredentials ;
dcp.DiscoverAny (wsdl);
dcp.ResolveAll( );
foreach (object osd in dcp.Documents.V alues)
{
if (osd is ServiceDescript ion)
sdi.AddServiceD escription((Ser viceDescription )osd, null, null);
if (osd is XmlSchema)
{
// store in global schemas variable
if (schemas == null) schemas = new XmlSchemas();
schemas.Add((Xm lSchema)osd);
sdi.Schemas.Add ((XmlSchema)osd );
}
}
sdi.ProtocolNam e = protocolName;
sdi.Import(cns, null);
// change the base class
// get all available Service classes - not only the default one
ArrayList newCtr = new ArrayList();
foreach (CodeTypeDeclar ation ctDecl in cns.Types)
{
if(ctDecl.BaseT ypes.Count > 0)
{
if(ctDecl.BaseT ypes[0].BaseType == CodeConstants.D EFAULTBASETYPE)
{
newCtr.Add(ctDe cl);
}
}
}
foreach (CodeTypeDeclar ation ctDecl in newCtr)
{
cns.Types.Remov e(ctDecl);
ctDecl.BaseType s[0] = new CodeTypeReferen ce
(CodeConstants. CUSTOMBASETYPE) ;
cns.Types.Add(c tDecl);
}
// source code generation
CSharpCodeProvi der cscp = new CSharpCodeProvi der();
ICodeGenerator icg = cscp.CreateGene rator();
StringBuilder srcStringBuilde r = new StringBuilder() ;
StringWriter sw = new StringWriter(sr cStringBuilder,
CultureInfo.Cur rentCulture);
if (schemas != null)
{
foreach (XmlSchema xsd in schemas)
{
if (XmlSchemas.IsD ataSet(xsd))
{
MemoryStream mem = new MemoryStream();
mem.Position = 0;
xsd.Write(mem);
mem.Position = 0;
DataSet dataSet1 = new DataSet();
dataSet1.Locale = CultureInfo.Inv ariantCulture;
dataSet1.ReadXm lSchema(mem);
TypedDataSetGen erator.Generate (dataSet1, cns, icg);
}
}
}
icg.GenerateCod eFromNamespace( cns, sw, null);
proxySource = srcStringBuilde r.ToString();
sw.Close();
// assembly compilation
string location = "";
if(HttpContext. Current != null)
{
location = HttpContext.Cur rent.Server.Map Path(".");
location += @"\bin\";
}
CompilerParamet ers cp = new CompilerParamet ers();
cp.ReferencedAs semblies.Add("S ystem.dll");
cp.ReferencedAs semblies.Add("S ystem.Xml.dll") ;
cp.ReferencedAs semblies.Add("S ystem.Web.Servi ces.dll");
cp.ReferencedAs semblies.Add("S ystem.Data.dll" );
cp.ReferencedAs semblies.Add(As sembly.GetExecu tingAssembly(). Location);
cp.ReferencedAs semblies.Add(lo cation +
"Thinktecture.T ools.Web.Servic es.Extensions.M essages.dll");
cp.GenerateExec utable = false;
cp.GenerateInMe mory = false;
cp.IncludeDebug Information = false;
cp.TempFiles = new
TempFileCollect ion(CompiledAss emblyCache.GetL ibTempPath());
ICodeCompiler icc = cscp.CreateComp iler();
CompilerResults cr = icc.CompileAsse mblyFromSource( cp, proxySource);
if(cr.Errors.Co unt > 0)
throw new
DynamicCompilat ionException(st ring.Format(Cul tureInfo.Curren tCulture,
@"Building dynamic assembly failed: {0} errors - {1}",
cr.Errors.Count ,cr.Errors[0].ErrorText ));
Assembly compiledAssembl y = cr.CompiledAsse mbly;
//rename temporary assembly in order to cache it for later use
CompiledAssembl yCache.RenameTe mpAssembly(cr.P athToAssembly, wsdl);
return compiledAssembl y;
It creates a proxyInstance (as an object where objTypeName is passed
in):
foreach (Type ty in ProxyAssembly.G etTypes())
{
if(ty.BaseType == typeof(SoapHttp ClientProtocolE xtended))
{
if(objTypeName == null || objTypeName.Len gth == 0 || ty.Name ==
objTypeName)
{
objTypeName = ty.Name;
break;
}
}
}
Type t = ass.GetType(Cod eConstants.CODE NAMESPACE + "." + objTypeName);
return Activator.Creat eInstance(t);
And it uses this to call the method:
MethodInfo mi = proxyInstance.G etType().GetMet hod(methodName) ;
object[] paramsArray = (object[])methodParams.T oArray(typeof(o bject));
object result = mi.Invoke(proxy Instance, paramsArray);
But the Invoke fails with Access denied. Using this base (because I
have downloaded it and have not had the expertise or time to fully
understand it) - how can I get this to work with Anonymous Access
turned off. I imagine if I can get an IWebProxy object from
proxyInstance I could try setting the credentials of that before Invoke
but I'm not sure how to get that object.
Any ideas>
TIA
Phil
Hi,
I am using a 3rd party product to create dynamic web service proxies
(downloaded from www.thinktecture.com).
My problem is that it doesn't work if Anonymous Access is turned off. I
know, from searching, that this is to do with Credentials but I just
can't seem to find where to fix it.
First it builds wsdl string (I added the 2 Credentials lines):
WebRequest req = WebRequest.Crea te(uri);
req.Credentials = System.Net.Cred entialCache.Def aultCredentials ;
WebResponse result = req.GetResponse ();
Stream ReceiveStream = result.GetRespo nseStream();
Encoding encode = Encoding.GetEnc oding("utf-8");
StreamReader sr = new StreamReader(Re ceiveStream, encode);
string wsdlSourceValue = sr.ReadToEnd();
sr.Close();
return wsdlSourceValue ;
Then it builds an assembly:
StringReader wsdlStringReade r = new StringReader(st rWsdl);
XmlTextReader tr = new XmlTextReader(w sdlStringReader );
ServiceDescript ion.Read(tr);
tr.Close();
// WSDL service description importer
CodeNamespace cns = new CodeNamespace(C odeConstants.CO DENAMESPACE);
sdi = new ServiceDescript ionImporter();
// check for optional imports in the root WSDL
DiscoveryClient Protocol dcp = new DiscoveryClient Protocol();
//(I added this credentials line)
dcp.Credentials = System.Net.Cred entialCache.Def aultCredentials ;
dcp.DiscoverAny (wsdl);
dcp.ResolveAll( );
foreach (object osd in dcp.Documents.V alues)
{
if (osd is ServiceDescript ion)
sdi.AddServiceD escription((Ser viceDescription )osd, null, null);
if (osd is XmlSchema)
{
// store in global schemas variable
if (schemas == null) schemas = new XmlSchemas();
schemas.Add((Xm lSchema)osd);
sdi.Schemas.Add ((XmlSchema)osd );
}
}
sdi.ProtocolNam e = protocolName;
sdi.Import(cns, null);
// change the base class
// get all available Service classes - not only the default one
ArrayList newCtr = new ArrayList();
foreach (CodeTypeDeclar ation ctDecl in cns.Types)
{
if(ctDecl.BaseT ypes.Count > 0)
{
if(ctDecl.BaseT ypes[0].BaseType == CodeConstants.D EFAULTBASETYPE)
{
newCtr.Add(ctDe cl);
}
}
}
foreach (CodeTypeDeclar ation ctDecl in newCtr)
{
cns.Types.Remov e(ctDecl);
ctDecl.BaseType s[0] = new CodeTypeReferen ce
(CodeConstants. CUSTOMBASETYPE) ;
cns.Types.Add(c tDecl);
}
// source code generation
CSharpCodeProvi der cscp = new CSharpCodeProvi der();
ICodeGenerator icg = cscp.CreateGene rator();
StringBuilder srcStringBuilde r = new StringBuilder() ;
StringWriter sw = new StringWriter(sr cStringBuilder,
CultureInfo.Cur rentCulture);
if (schemas != null)
{
foreach (XmlSchema xsd in schemas)
{
if (XmlSchemas.IsD ataSet(xsd))
{
MemoryStream mem = new MemoryStream();
mem.Position = 0;
xsd.Write(mem);
mem.Position = 0;
DataSet dataSet1 = new DataSet();
dataSet1.Locale = CultureInfo.Inv ariantCulture;
dataSet1.ReadXm lSchema(mem);
TypedDataSetGen erator.Generate (dataSet1, cns, icg);
}
}
}
icg.GenerateCod eFromNamespace( cns, sw, null);
proxySource = srcStringBuilde r.ToString();
sw.Close();
// assembly compilation
string location = "";
if(HttpContext. Current != null)
{
location = HttpContext.Cur rent.Server.Map Path(".");
location += @"\bin\";
}
CompilerParamet ers cp = new CompilerParamet ers();
cp.ReferencedAs semblies.Add("S ystem.dll");
cp.ReferencedAs semblies.Add("S ystem.Xml.dll") ;
cp.ReferencedAs semblies.Add("S ystem.Web.Servi ces.dll");
cp.ReferencedAs semblies.Add("S ystem.Data.dll" );
cp.ReferencedAs semblies.Add(As sembly.GetExecu tingAssembly(). Location);
cp.ReferencedAs semblies.Add(lo cation +
"Thinktecture.T ools.Web.Servic es.Extensions.M essages.dll");
cp.GenerateExec utable = false;
cp.GenerateInMe mory = false;
cp.IncludeDebug Information = false;
cp.TempFiles = new
TempFileCollect ion(CompiledAss emblyCache.GetL ibTempPath());
ICodeCompiler icc = cscp.CreateComp iler();
CompilerResults cr = icc.CompileAsse mblyFromSource( cp, proxySource);
if(cr.Errors.Co unt > 0)
throw new
DynamicCompilat ionException(st ring.Format(Cul tureInfo.Curren tCulture,
@"Building dynamic assembly failed: {0} errors - {1}",
cr.Errors.Count ,cr.Errors[0].ErrorText ));
Assembly compiledAssembl y = cr.CompiledAsse mbly;
//rename temporary assembly in order to cache it for later use
CompiledAssembl yCache.RenameTe mpAssembly(cr.P athToAssembly, wsdl);
return compiledAssembl y;
It creates a proxyInstance (as an object where objTypeName is passed
in):
foreach (Type ty in ProxyAssembly.G etTypes())
{
if(ty.BaseType == typeof(SoapHttp ClientProtocolE xtended))
{
if(objTypeName == null || objTypeName.Len gth == 0 || ty.Name ==
objTypeName)
{
objTypeName = ty.Name;
break;
}
}
}
Type t = ass.GetType(Cod eConstants.CODE NAMESPACE + "." + objTypeName);
return Activator.Creat eInstance(t);
And it uses this to call the method:
MethodInfo mi = proxyInstance.G etType().GetMet hod(methodName) ;
object[] paramsArray = (object[])methodParams.T oArray(typeof(o bject));
object result = mi.Invoke(proxy Instance, paramsArray);
But the Invoke fails with Access denied. Using this base (because I
have downloaded it and have not had the expertise or time to fully
understand it) - how can I get this to work with Anonymous Access
turned off. I imagine if I can get an IWebProxy object from
proxyInstance I could try setting the credentials of that before Invoke
but I'm not sure how to get that object.
Any ideas>
TIA
Phil