Question

I have a class in a web service that has a nested class in it.

namespace MyWS
{
    // web service class with web methods etc. here

    public class SomeClass
    {
        // fields, properties, stuff etc.

        public class NestedClass
        {
            // ...
        }
    }
}

When I try to make a web reference out of the web service, NestedClass is no longer nested and is outside of SomeClass as a standalone class in the reference (accessed through MyWS.NestedClass). Is there any way to force it to stay nested when web reference is generated next time? Some kind of attribute maybe?

I'm using .NET 2.0 and VS 2005, if it makes any difference. If possible please don't suggest upgrading to newer technology/environment as a solution.

EDIT: It's not a matter of architecture. I don't really need the class to be nested there and I can easily change it so it makes more sense. I just wonder if it's possible to force it to be nested in the reference if I wanted to.

Was it helpful?

Solution

When you define a service like this, it gets translated to WSDL first. That WSDL is then imported in the client tools and used to create classes and code in the importing language.

Now consider a language that does not support nesting of Classes, what should happen there?

A Service definition is limited to a common denominator of language features. Nested types are not part of the feature set.

There are other C# features that you can't (effectively) use.

OTHER TIPS

This is more architectual problem, then technological, imo.

In your case I see you declare nested class public. There are no much reasons to do that, if not artificially, or declaratively, create a dependency between those 2 types. But this can be done in other way, avoiding nesting, for example:

public class A {
}

public class Dependend {
    //The only available construstor of Dependent is that one which 
    //accepts A type
    public Dependend(A aType) {
    }
}

In this case you have 2 distinct entities (so it's easier to interact with them) and you also construct a strong relationship between them.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top