P
phancey
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.Create(uri);
req.Credentials = System.Net.CredentialCache.DefaultCredentials;
WebResponse result = req.GetResponse();
Stream ReceiveStream = result.GetResponseStream();
Encoding encode = Encoding.GetEncoding("utf-8");
StreamReader sr = new StreamReader(ReceiveStream, encode);
string wsdlSourceValue = sr.ReadToEnd();
sr.Close();
return wsdlSourceValue;
Then it builds an assembly:
StringReader wsdlStringReader = new StringReader(strWsdl);
XmlTextReader tr = new XmlTextReader(wsdlStringReader);
ServiceDescription.Read(tr);
tr.Close();
// WSDL service description importer
CodeNamespace cns = new CodeNamespace(CodeConstants.CODENAMESPACE);
sdi = new ServiceDescriptionImporter();
// check for optional imports in the root WSDL
DiscoveryClientProtocol dcp = new DiscoveryClientProtocol();
//(I added this credentials line)
dcp.Credentials = System.Net.CredentialCache.DefaultCredentials;
dcp.DiscoverAny(wsdl);
dcp.ResolveAll();
foreach (object osd in dcp.Documents.Values)
{
if (osd is ServiceDescription)
sdi.AddServiceDescription((ServiceDescription)osd, null, null);
if (osd is XmlSchema)
{
// store in global schemas variable
if (schemas == null) schemas = new XmlSchemas();
schemas.Add((XmlSchema)osd);
sdi.Schemas.Add((XmlSchema)osd);
}
}
sdi.ProtocolName = protocolName;
sdi.Import(cns, null);
// change the base class
// get all available Service classes - not only the default one
ArrayList newCtr = new ArrayList();
foreach (CodeTypeDeclaration ctDecl in cns.Types)
{
if(ctDecl.BaseTypes.Count > 0)
{
if(ctDecl.BaseTypes[0].BaseType == CodeConstants.DEFAULTBASETYPE)
{
newCtr.Add(ctDecl);
}
}
}
foreach (CodeTypeDeclaration ctDecl in newCtr)
{
cns.Types.Remove(ctDecl);
ctDecl.BaseTypes[0] = new CodeTypeReference
(CodeConstants.CUSTOMBASETYPE);
cns.Types.Add(ctDecl);
}
// source code generation
CSharpCodeProvider cscp = new CSharpCodeProvider();
ICodeGenerator icg = cscp.CreateGenerator();
StringBuilder srcStringBuilder = new StringBuilder();
StringWriter sw = new StringWriter(srcStringBuilder,
CultureInfo.CurrentCulture);
if (schemas != null)
{
foreach (XmlSchema xsd in schemas)
{
if (XmlSchemas.IsDataSet(xsd))
{
MemoryStream mem = new MemoryStream();
mem.Position = 0;
xsd.Write(mem);
mem.Position = 0;
DataSet dataSet1 = new DataSet();
dataSet1.Locale = CultureInfo.InvariantCulture;
dataSet1.ReadXmlSchema(mem);
TypedDataSetGenerator.Generate(dataSet1, cns, icg);
}
}
}
icg.GenerateCodeFromNamespace(cns, sw, null);
proxySource = srcStringBuilder.ToString();
sw.Close();
// assembly compilation
string location = "";
if(HttpContext.Current != null)
{
location = HttpContext.Current.Server.MapPath(".");
location += @"\bin\";
}
CompilerParameters cp = new CompilerParameters();
cp.ReferencedAssemblies.Add("System.dll");
cp.ReferencedAssemblies.Add("System.Xml.dll");
cp.ReferencedAssemblies.Add("System.Web.Services.dll");
cp.ReferencedAssemblies.Add("System.Data.dll");
cp.ReferencedAssemblies.Add(Assembly.GetExecutingAssembly().Location);
cp.ReferencedAssemblies.Add(location +
"Thinktecture.Tools.Web.Services.Extensions.Messages.dll");
cp.GenerateExecutable = false;
cp.GenerateInMemory = false;
cp.IncludeDebugInformation = false;
cp.TempFiles = new
TempFileCollection(CompiledAssemblyCache.GetLibTempPath());
ICodeCompiler icc = cscp.CreateCompiler();
CompilerResults cr = icc.CompileAssemblyFromSource(cp, proxySource);
if(cr.Errors.Count > 0)
throw new
DynamicCompilationException(string.Format(CultureInfo.CurrentCulture,
@"Building dynamic assembly failed: {0} errors - {1}",
cr.Errors.Count,cr.Errors[0].ErrorText ));
Assembly compiledAssembly = cr.CompiledAssembly;
//rename temporary assembly in order to cache it for later use
CompiledAssemblyCache.RenameTempAssembly(cr.PathToAssembly, wsdl);
return compiledAssembly;
It creates a proxyInstance (as an object where objTypeName is passed
in):
foreach (Type ty in ProxyAssembly.GetTypes())
{
if(ty.BaseType == typeof(SoapHttpClientProtocolExtended))
{
if(objTypeName == null || objTypeName.Length == 0 || ty.Name ==
objTypeName)
{
objTypeName = ty.Name;
break;
}
}
}
Type t = ass.GetType(CodeConstants.CODENAMESPACE + "." + objTypeName);
return Activator.CreateInstance(t);
And it uses this to call the method:
MethodInfo mi = proxyInstance.GetType().GetMethod(methodName);
object[] paramsArray = (object[])methodParams.ToArray(typeof(object));
object result = mi.Invoke(proxyInstance, 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
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.Create(uri);
req.Credentials = System.Net.CredentialCache.DefaultCredentials;
WebResponse result = req.GetResponse();
Stream ReceiveStream = result.GetResponseStream();
Encoding encode = Encoding.GetEncoding("utf-8");
StreamReader sr = new StreamReader(ReceiveStream, encode);
string wsdlSourceValue = sr.ReadToEnd();
sr.Close();
return wsdlSourceValue;
Then it builds an assembly:
StringReader wsdlStringReader = new StringReader(strWsdl);
XmlTextReader tr = new XmlTextReader(wsdlStringReader);
ServiceDescription.Read(tr);
tr.Close();
// WSDL service description importer
CodeNamespace cns = new CodeNamespace(CodeConstants.CODENAMESPACE);
sdi = new ServiceDescriptionImporter();
// check for optional imports in the root WSDL
DiscoveryClientProtocol dcp = new DiscoveryClientProtocol();
//(I added this credentials line)
dcp.Credentials = System.Net.CredentialCache.DefaultCredentials;
dcp.DiscoverAny(wsdl);
dcp.ResolveAll();
foreach (object osd in dcp.Documents.Values)
{
if (osd is ServiceDescription)
sdi.AddServiceDescription((ServiceDescription)osd, null, null);
if (osd is XmlSchema)
{
// store in global schemas variable
if (schemas == null) schemas = new XmlSchemas();
schemas.Add((XmlSchema)osd);
sdi.Schemas.Add((XmlSchema)osd);
}
}
sdi.ProtocolName = protocolName;
sdi.Import(cns, null);
// change the base class
// get all available Service classes - not only the default one
ArrayList newCtr = new ArrayList();
foreach (CodeTypeDeclaration ctDecl in cns.Types)
{
if(ctDecl.BaseTypes.Count > 0)
{
if(ctDecl.BaseTypes[0].BaseType == CodeConstants.DEFAULTBASETYPE)
{
newCtr.Add(ctDecl);
}
}
}
foreach (CodeTypeDeclaration ctDecl in newCtr)
{
cns.Types.Remove(ctDecl);
ctDecl.BaseTypes[0] = new CodeTypeReference
(CodeConstants.CUSTOMBASETYPE);
cns.Types.Add(ctDecl);
}
// source code generation
CSharpCodeProvider cscp = new CSharpCodeProvider();
ICodeGenerator icg = cscp.CreateGenerator();
StringBuilder srcStringBuilder = new StringBuilder();
StringWriter sw = new StringWriter(srcStringBuilder,
CultureInfo.CurrentCulture);
if (schemas != null)
{
foreach (XmlSchema xsd in schemas)
{
if (XmlSchemas.IsDataSet(xsd))
{
MemoryStream mem = new MemoryStream();
mem.Position = 0;
xsd.Write(mem);
mem.Position = 0;
DataSet dataSet1 = new DataSet();
dataSet1.Locale = CultureInfo.InvariantCulture;
dataSet1.ReadXmlSchema(mem);
TypedDataSetGenerator.Generate(dataSet1, cns, icg);
}
}
}
icg.GenerateCodeFromNamespace(cns, sw, null);
proxySource = srcStringBuilder.ToString();
sw.Close();
// assembly compilation
string location = "";
if(HttpContext.Current != null)
{
location = HttpContext.Current.Server.MapPath(".");
location += @"\bin\";
}
CompilerParameters cp = new CompilerParameters();
cp.ReferencedAssemblies.Add("System.dll");
cp.ReferencedAssemblies.Add("System.Xml.dll");
cp.ReferencedAssemblies.Add("System.Web.Services.dll");
cp.ReferencedAssemblies.Add("System.Data.dll");
cp.ReferencedAssemblies.Add(Assembly.GetExecutingAssembly().Location);
cp.ReferencedAssemblies.Add(location +
"Thinktecture.Tools.Web.Services.Extensions.Messages.dll");
cp.GenerateExecutable = false;
cp.GenerateInMemory = false;
cp.IncludeDebugInformation = false;
cp.TempFiles = new
TempFileCollection(CompiledAssemblyCache.GetLibTempPath());
ICodeCompiler icc = cscp.CreateCompiler();
CompilerResults cr = icc.CompileAssemblyFromSource(cp, proxySource);
if(cr.Errors.Count > 0)
throw new
DynamicCompilationException(string.Format(CultureInfo.CurrentCulture,
@"Building dynamic assembly failed: {0} errors - {1}",
cr.Errors.Count,cr.Errors[0].ErrorText ));
Assembly compiledAssembly = cr.CompiledAssembly;
//rename temporary assembly in order to cache it for later use
CompiledAssemblyCache.RenameTempAssembly(cr.PathToAssembly, wsdl);
return compiledAssembly;
It creates a proxyInstance (as an object where objTypeName is passed
in):
foreach (Type ty in ProxyAssembly.GetTypes())
{
if(ty.BaseType == typeof(SoapHttpClientProtocolExtended))
{
if(objTypeName == null || objTypeName.Length == 0 || ty.Name ==
objTypeName)
{
objTypeName = ty.Name;
break;
}
}
}
Type t = ass.GetType(CodeConstants.CODENAMESPACE + "." + objTypeName);
return Activator.CreateInstance(t);
And it uses this to call the method:
MethodInfo mi = proxyInstance.GetType().GetMethod(methodName);
object[] paramsArray = (object[])methodParams.ToArray(typeof(object));
object result = mi.Invoke(proxyInstance, 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